poniedziałek, 12 września 2011

INITIALIZATION BLOCKS

1. It is legal to create instance of outer class within both: static and instance initialization blocks.
    E.g.:

    class X {
         { X x = new X();}
          static { X x = new X();}
      }

2. It is legal to use this in initialization (non-static) block:

    class X {

        int a;
       {this.a = 4;}
    }

3.

   class X{
       {x = 4}
        int x;
     
        public X() {
           System.out.println(x);         //prints 4
         }
    }



     class X{
       {x = 4}
        int x = 1;
     
        public X() {
           System.out.println(x);         //prints 1
         }
    }



     class X{

        int x=1;
       {x = 4}

        public X() {
           System.out.println(x);         //prints 4
         }
    }

   exactly the same is in case of static initialization blocks

Brak komentarzy:

Prześlij komentarz