Win a copy of Rust Web Development this week in the Other Languages 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

need simple help

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain why does it give me 0 as output ?
Note : even if i dont Initialize j (like private int j; ) does give same output.
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);
}
}
thanks
[This message has been edited by Vivek Shrivastava (edited June 05, 2000).]
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variables are initialized in the order of which they are called.Here when the method giveMeJ() is called to initialize the variable i,j has not yet been assigned any value and
by default it has got a value of 0,so thats the main reason why 0 is printed.
If the sequence is changed and j is moved up like this
private int j = 10;
private int i = giveMeJ();
Then it will print out 10.
If the variable j is declared as static then the order should not matter here since Static initialization takes place only once as the class object is loaded for the first time.(or) you access the static member of the class (even if u don't make an object of that class).So if j is declared as static then irrespective of the sequence it will print out 10.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, first things first. Uninitialized class-level primitive variables are always initialized to their default values. ie., 0 for int, 0.0 for double, false for boolean and so on. So, if you were to comment out giveMeJ() method call, you will get zero. i is uninitialized, so gets it default value 0.
Now about your question. Remember that declaration and initialization and are done in the order they appear in the code. In your code, giveMeJ() method is trying to access integer j before it is initialized. So, when the giveMeJ() method is getting executed, value of j is its default value which is zero.. If you swap the declaration of i and j in your code, it prints 10. I hope now you know why it prints 10.
The interesting thing is, if you replace the call to giveMeJ() with j, compiler gives you an error saying Can't make forward reference to j. But for some reason best known to authors of Java, they thought situations like your code is valid.
Hope this helps,
Ajith
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS!!! I was racing with Surya!!...
Ajith
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes ajith i was also confuse with concept of forward refrencing of the variable because i was trying to use same thing here and was expecting a compiler error.
i think it is better to close it.
thanks ajith and surya.
vivek
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Ajith, this is one reason I post so little nowadays - there's usually someone else who will post a complete answer before me.
 
reply
    Bookmark Topic Watch Topic
  • New Topic