• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Little challenge

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What the result of java C and why ?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good question. It needs a good appreciation of when and what happens when classes are loaded.

Try to think it through and then run it, if necessary with some System.out.println statements added.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the static Initializer block of B running before that of A?
Does B.f() make static Initializer block of B to complete first and then resumes the execution of satic Initializer in A?
[ June 30, 2006: Message edited by: pramila ch ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, calling B.f() inside the static initializer of A forces B to be initialized before A is finished.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


output is--->>
A.i --> true
B.i --> false

When you are running C.main(), in first SOP, A.i is called. So JVM first load class A, then it will initialize class A.

1. i gets false.
2. static block gets executed. There it encounters B.f(); so it stops execution at that point, start loading class B.

When B gets loaded and initialized then...

1. i is assigned to false.
2. Its static block gets executed. In static block of B, you are assigning the value of i of A in i of B class. Upto this point, i of A is false. So B.i becomes false. Now class B is loaded and initialised. Now control will resume from the static block of class A, calling f() does nothing.
After calling f(), i of A is set to true.

So when both class gets initialized, A.i becomes true.
and B.i becomes false.

Naseem
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member variables marked final are not initialized by default. Final variable should be assigned value ONLY once.
In Class A, if a SOP to print i is placed before calling B.f(), a compile error will result since i is not initialised.

The whole explaination make sense except when A.i is initialised.

What I don't understand is when class B is loaded, A.i is somehow being initialised to false. A.i is only initialised after class B is loaded.


Thanks,
Eleanor Leong
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What I don't understand is when class B is loaded



When B.f() is called. At that time B is loaded i.e., when class A is in initialization process. At that time B gets loaded and initialized. Then initialization of A will complete.

Regards

Naseem
[ June 30, 2006: Message edited by: Naseem Khan ]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naseem,

Even I am not able to understand final variable are not given default value.
So in this case how A.i is getting false value.

Gaurav
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable i in A is called a blank final variable. It must be definitely assigned by the end of the static initializer.

i is given its default value for accesses that happen (like when B access A.i) and is given its value at the end of the static initializer.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I modified the code and added some SOPs to understand the flow of execution. I am giving the code here and also the output.

class A {
final static boolean i;

static {
System.out.println("in static A before callingB.f()");
B.f();
System.out.println("in static A before initialzation of A.i");
i = true;
System.out.println("in static A after initialzation of A.i, A.i= "+i);
}

}

class B {
final static boolean i;

static void f() {
}

static {
System.out.println("in static B before initialzation of B.i, A.i = " + A.i);
i = A.i;
System.out.println("in static B after initialzation of B.i, A.i = " + A.i + " B.i = " + i);
}

}


class C {

public static void main(String[] args) {
System.out.println("A.i --> "+A.i);
System.out.println("B.i --> "+B.i);
}
}

The output is:

in static A before callingB.f()
in static B before initialzation of B.i, A.i = false
in static B after initialzation of B.i, A.i = false B.i = false
in static A before initialzation of A.i
in static A after initialzation of A.i, A.i= true
A.i --> true
B.i --> false


Still I cannot understand how can we use the value of A.i in class B before initialization.

And when I try to modify line 7 to
System.out.println("in static A before initialzation of A.i = " + i);
it gives me error (which is quite natural ofcourse).
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone

It's quite an interesting question that has been put in this thread, and I have one weird observation to add: If we try to print the field i in lines 5 or 7 in Harshad's version of the program, then compilation fails, but if we instead try to print A.i, then the program compiles and "false" is printed.

I must admit that I'm a bit baffled. Why does A.i work while i doesn't?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once declared, identifiers can be used freely. Using an identifier before its declaration is called a forward reference. If an instance variable tries to forward reference another instance variable during initiaization, the result in a compile-time error.
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naseem's explanation stands. page 57 in the K&B states "Decalaring a variable with the final keyword makes it impossible to reinitialize that variable once it has been initialized with an explicit value(notice we said explicit rather than default)." so that means that final variables do get default values. and if it has received a default it can be reinitialized.
 
Eleanor Leong
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amitabha,
Your coding does not mention about the default values either.
The following code is a test on intialialization final instance variable :
class A {
final static boolean i;
final int Fint;
int Aint;
{
System.out.println("Cls A b4 init-> Fint : " + Fint);//Line A
Fint = 100;
System.out.println("Cls A After init-> Fint : " + Fint); //Line B
System.out.println("Cls A b4 init-> Aint : " + Aint); // Line C
Aint = 200;
System.out.println("Class A Af init -> Aint : " + Aint);
}
}
class TClass {
public static void main(String[] args) {
A AClass = new A();
}
}

You can notice Line A which prints a final instance variable causes a compile error of un-initialization of Final variable Fint. Line B is compiled OK for printing a instance variable.

I am buzzled like Morten. Looks like final static variable is treated differently from final variable.

Why does A.i work while i doesn't until i is initialised to true? javascript: x()
banghead

I am trying to understand Wise Owen's commends but still don't help. I know Wise Owen has a lot of reference. It would be more help if he can be more specific as explaining how his comments is related to the question instead of just quoting references.javascript: x()
jumpingjoy

Thanks,
Eleanor
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm....I think...maybe... that when you have more than 1 class, as in the original code, java being bottom-up compilation will compile the static initializer of class B first, and since in that block we are initializing B's i with the valueof A's i. it gives it a default value. the jvm though loads static initialization blocks top down before it interprets anything else but by then the program has already been made and values set.
Im sure this is probably wrong but that how i would explain it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic