• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

a question regrading new operator

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
given the following sample code:
class A{
int i=getJ();
int j=10;
int getJ(){return j;}
public static void main(String[] args){
A a = new A();
System.out.println("i="+a.i+" j="+a.j);
}
}
Why does it output that i is 0, rather than 10?
thanks!
[ September 22, 2002: Message edited by: Li Wenfeng ]
[ September 24, 2002: Message edited by: Li Wenfeng ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
A look at the decompiled class file shows a few secrets...
Here is the constructor for your program once the compiler gets its filthy mitts on it:
Method A()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 aload_0
5 aload_0
6 invokevirtual #2 <Method int getJ()>
9 putfield #3 <Field int i>
12 aload_0
13 bipush 10
15 putfield #4 <Field int j>
18 return
Now, ignoring the convoluted garble, check out the order. putfield is where a value is assigned into a variable. Also note where "j" is actually set... line 13 and 15 where the integer 10 is pushed and put into field 4. So what's in field j when the method getJ() is called? Nothing? Why? Because all of the assignments happen in order.
When an instance is constructed, the constuctor's parent constructor is called first (see line 1 in the decompile?). After that, the instance variables are all initialised IN ORDER OF DEFINITION. Then the instance blocks are run, and after that, the actual constructor code. Its important to note this order, and also the guarantee that everything will be defined when you try to run code - you will never get an uninitialised error when creating an instance because the specification simply won't let you. However, it has set up j with a zero value before it gets around to putting in the stuff you want in it. This is why "i" will return zero.
Hope that helps!
Jeremy
[ September 24, 2002: Message edited by: Jeremy Hynoski ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, the code will not compile, as you are referencing instance variables i and j from a static method.


The code does compile and run as posted. The reason i is printing 0 is as follows (in my mind):
1. In first pass (when Object is being create), compiler is will define and assign default values to all member variables. So i and j both get zeros.
2 In second pass, compiler will actually evaluate the programmer's assigned value for each member, in the textual order. While doing so for i, it needs the value of j. At that time j is zero, so i gets 0. Next, j is assined 10. If you put a print statement after it, you see j is 10.
Hope this helps
Barkat
 
Li Wenfeng
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks greatly for your warmhearted replies!
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think you meant: why does i print 0 instead of 10 ?
The i's initializer expression gets excuted be j's initialization to 10 and at that time the method getJ() returns the default value of J which is 0.
N.B
Before executing any intializer expression, instance members are constructed and assigned to their default values.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic