Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method invoke

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, there
could anyone explain to me why the following code output is 0 instead of 10?
public class AQuestion3
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion3()).i);
}
}
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the JVM creates the object it executes the initialization of instance variables before the code in the constructor (roughly) and it does so in textual order: when initializing i the value of j is still cero, because this the default value for any numeric field. Thus the method returns cero and that is assigned to i. Latter j is initialized to 10, and from them an access to j will yield that value but not before. To check it out place j=10 before i declaration.
Please read the following because the order of initialization is very important http://codeguru.earthweb.com/java/tij/tij0052.shtml#Heading153
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has to do with the initialization flow. When you build a new AQuestion3 instance, the following happens:
integer i is declared and initialized, thus giveMeJ is invoked but at that time j still has the value 0 and thus the method returns 0 which gets assigned to i. That's all.
The initialization flow is from the top to the bottom of the class. if you put the second line (init of j) before the previous one, you'll get 10.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
fengqiao cao
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, guys
if what you said are right.
i think the following code will compile and print 0, however it cannot compile. why?
public class foster
{
private int i = j;
private int j = 10;
public static void main(String args[])
{
System.out.println((new foster()).i);
}
}
 
fengqiao cao
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry guys
you are right.
i think misunderstand the concept of compile and run.
could you explain or provide some refereces on topic about the job of compiling?
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is still very confusing. I understand the order of initialization but what I don't understand is why you don't get an "illegal forward reference" error as when you try:


It makes no sense to me how the "return j" in the original code is able to have a value of 0, yet in the above code I posted you get an illegal forward reference error?
Original Code:

It's almost as if during the time the compiler is taking to go to the method giveMeJ() that j is somehow initialized to 0 OR it's as if the compiler is going in the giveMeJ() method, "Hey, I need the value of j but it hasn't been set yet so give me the default value of an int which is 0." If the later is the case how come it can't do that in the 'illegal forward reference' code I posted? What am I missing here.
Thanks
Rick
[This message has been edited by Rick Reumann (edited November 08, 2001).]
[This message has been edited by Rick Reumann (edited November 08, 2001).]
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pertaining to this question .... consider the example below
public class AQuestion3
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
System.out.println( "Trying to initialize i" );
return j;
}
public static void main(String args[])
{
System.out.println( "b4 calling method" );
System.out.println((new AQuestion3()).i);
}
}
I should be seeing "Trying to initialize i" followed by "b4 calling method" right ???
[ how do i enable UBB code can somebody tell me ---Thanks ]

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Harsha,
how come this could be right can u explain ?
~Kumar
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For code posted by Harsha Jay;
I'd say you will see "b4 calling method" followed by "Trying to initialize i".
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rick


It's almost as if during the time the compiler is taking to go to the method giveMeJ() that j is somehow initialized to 0 OR it's as if the compiler is going in the giveMeJ() method, "Hey, I need the value of j but it hasn't been set yet so give me the default value of an int which is 0." If the later is the case how come it can't do that in the 'illegal forward reference' code I posted? What am I missing here.


Not really, the compiler doesn't execute the method. It doesn't know that the method returns j. It makes certain that the type of the variable returned is asignable compatible with the declared return value for the method. It doesn't look if j was initialized or not.
Hi Harsha


I should be seeing "Trying to initialize i" followed by "b4 calling method" right ???


No because the object is created after the printing of b4 message. Pleae red the url I gave. Probably it will solve all your doubts
To use UBB: http://www.javaranch.com/ubb/ubbcode.html
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic