This is some information on the Java 6 certification that I prepare. This information is not very well structured and it is most of the time an answer to the questions that come when I read the SCJP 6 book (Bates and Sierra). *** AS OF 5 OF MARCH 2010, I AM SCJP 6.0 ***

Monday, January 4, 2010

1 - Scope

  • The following code will compile !
public class Test1 {

    int a=0;

    void method1(int a0) {

        int a1=0;

        { int a=0;}

        for(int a=0;a < 10; a++) {
            System.out.println("" + a);
        }

        for(int a=0;a < 10; a++) {
            System.out.println("" + a);
        }

    }

}
The declaration of the integer in the block of code surrounded by {} and in the 2 for will NOT interfere !

    • But if you change the name of the method parameter "a0" to "a" OR the name of the local variable "a1" to "a" the method will not compile anymore ! The compiler will complain that "a" hides a field OR that "a" is already defined !
    • Could you notice that in the case you didn't rename "a0" and "a1" that the "int a" in the code will hide the instance variable !

    No comments:

    Post a Comment

    Followers