Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Static Variables Compilation

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic