aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Little challenge Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Little challenge" Watch "Little challenge" New topic
Author

Little challenge

Gael Lalire
Greenhorn

Joined: Jun 30, 2006
Posts: 1


What the result of java C and why ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Pramila Chinguru
Ranch Hand

Joined: May 05, 2006
Posts: 54
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 ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
Yes, calling B.f() inside the static initializer of A forces B to be initialized before A is finished.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


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


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Eleanor Leong
Greenhorn

Joined: Mar 24, 2006
Posts: 21
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

Joined: Apr 25, 2005
Posts: 809

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 ]
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
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

Joined: Feb 07, 2005
Posts: 2341
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.
Harshad Khasnis
Ranch Hand

Joined: Jun 06, 2006
Posts: 48
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).
Morten Monrad Pedersen
Ranch Hand

Joined: May 15, 2006
Posts: 31
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?
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
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.
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
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

Joined: Mar 24, 2006
Posts: 21
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

Joined: Mar 04, 2006
Posts: 361
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Little challenge
 
Similar Threads
Xplain me plz..
Please explain O/P ...
volatile... is this not correct way?
late binding
Question on inheritance