| Author |
Static Variables Compilation
|
Hari Mohan
Greenhorn
Joined: Jul 30, 2008
Posts: 6
|
|
Hi, please look at the code snippets below........ a.)
public class Test{ private int i = j; private int j = 10; public static void main( String args[]){ System.out.println((new Test()).i); } }
throws a compilation error b.)
public class Test{ private int i = j; private static int j = 10; public static void main(String args[]){ System.out.println((new Test()).i); } }
compiles well and gives an output 10; c.)
public class Test{ private static int i = j; private static int j = 10; public static void main(String args[]){ System.out.println((new Test()).i); } }
once again doesn't compile d.)
public class Test{ private int i = giveMeJ(); private int j = 10; private int giveMeJ(){ return j; } public static void main(String args[]){ System.out.println((new Test()).i); } }
compiles fine but gives an output 0 (???) e.)
public class Test{ private int i = giveMeJ(); private static int j = 10; private static int giveMeJ(){ return j; } public static void main(String args[]){ System.out.println((new Test()).i); } }
gives me 10 and finally f)
public class Test{ private static int i = giveMeJ(); private static int j = 10; private static int giveMeJ(){ return j; } public static void main(String args[]){ System.out.println((new Test()).i); } }
gives me 0; Can somebody please explain me the reasons for codes working in the way shown?
|
Keep Rocking,<br />HMV
|
 |
Ronald Schild
Ranch Hand
Joined: Jun 09, 2008
Posts: 117
|
|
A and C are trying to use j before it exists. Consider the code: This doesn't work. The variable i must be declared and initialized before it is used. Instance variables are initialized to a default value, but you must declare them still. B works, because here j is a static variable. This means that it is a class variable and is initialized when the class is initialized. This is before any object instantiation, and so before i exists. D works. The variable j is declared and exists so it can be used in method giveMeJ(). At the point where it is called to assign i, j has not yet been set to the value 10, so the default value 0 is returned. At E, j was initialized during class initialization. It works like D but now returns 10. For F I think you can deduct the right answer based on D.
|
Java hobbyist.
|
 |
Hari Mohan
Greenhorn
Joined: Jul 30, 2008
Posts: 6
|
|
Thank you Ronald....The only confusion I had was with (d). But now, it's clear, and as you had explained, The code below compiles and gives an output 10 because j has been initialized even before i was assigned with j returned by giveMeJ........That fits.....Thank you so much....
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
From Ronalds explanation can I deduce the initialization steps for a class like this : 1 -- > Class variables initialized by default value 2 -- > Instance variables initialized by default value 3 -- > Class Variables gets defined by there hard coded values. 4 -- > Instance Variables gets defined by there hard coded values. Correct me If I am wrong !
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
 |
|
|
subject: Static Variables Compilation
|
|
|