This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In this code here, the output is 0. Reason being y is not known when x was initialized. But, my question is -- I agree, y is not known at that time. But why is it not showing compiler error.. since, y is both declared and initialized after x? Also, can somebody please explain the correct flow of the program?
All the instance variables will get their default values when the super class object is created. And all the static varables will get there default values and if we initialized any values ,they will get at load time. But here how y got 0 value ?
Here y should get that values after executing this statement. private static int y = 5; if we put this statement private static int y ; or private static int y=0; then it will get that 0.
Y is 0 at the point when x is being inited, but subsequently it does get assigned to 5, which you can confirm by printing y's value directly in the main.
ASCII silly question, Get a silly ANSI.
Joined: Feb 28, 2007
Let me rewind the reel once again:
Explanation: Static variables are initialized when the class is first loaded so b=55 will be executed when the class is loaded. And when you say private int a=b; it will be executed when instance of the class is created, so compiler has no problem, when it would initialize a=b, b would already have value "55".
Explanation: Instance variables (static also) are initialized in the order they appear in the class definition. When the line a=b is executed, b is not known hence compilation error.
Case 3: (Your main concern)
Explanation: static variables (member variables too) are initialized in the order they appear in the class definition. When the line a=b is executed , b is not known to the compiler, hence compilation error.
Case 4: (Pay attention here)
It is allowed to call a method to initialize the class or member variable(see the example just before this when you got compiler error); But here compiler has no problem; But the compiler is not going to dig: (What is the value of y? ...) It will just return the default value, given to an int primitive. When the "private static int x = getValue();" line is executed, variable y is not initialized.
Got it Anil?
Joined: Feb 23, 2007
Chandra ---------------------------------------------------------------- It will just return the default value, given to an int primitive. ---------------------------------------------------------------
This is the line i could not understand properly
All instance varaible get their default value when object of its super class is created Right
And for static variable at the time of loading the class it will get their default value and if we initialize any value ,it will be assigned.
But here in this case at the time we call the getValue method means at load time ,no super object is created and this statement is also not execute private static int y=5 .
How the variable y got default value? Please tell me
Joined: Feb 28, 2007
Note: default value means "what is asigned to member (or static) variables" in case no explicit initialization is there.
byte gets 0 short get 0 int gets 0 float gets 0.0f double gets 0.0
And any reference variable gets null as default value.
What our lovingJava allows us: forward referencing when the used variable is static, that is true with our case. To your point:
When the above line is executed, no initialization of static variable "y" has been done because it comes next. But Java allows us forward referencing in this case so no error like "y not found"; but still Java is stick to its rule that "y" wont be initialized its explicit value that is "5" in our example, so therefore what to do, Complier has to allocate the memory, it can't leave member variables unassigned so therefore assign their default value. If you still dont get what default value is goto Note;
-goto is a keyword. A reserved word, not in use although.