Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Variables Compilation

 
Hari Mohan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hari Mohan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic