This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Why it's Zero?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends
I try to search the answer on Java Ranch & in different books also but i didn't find it.
Please can anybody tell me why the following program is printing 0.
class fooa
{
int i=meth();
int meth()
{
return 1;
}
}
class foob extends fooa
{
int i=2;
int meth()
{
return i;
}
}
class foo
{
public static void main(String args[])
{
fooa f = new foob();
System.out.println(f.i);
}
}

Thanx
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess to better understand it replace:
return i; with
return 9;
The return value will be 9, that is because meth() from class foob is executed when called from int i=meth(); in class fooa.
AH
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.
first thing first. your variable i is declared in fooa and foob.
variables are not 'late-binded'. consider the following code.
foob fb = new foob();
fooa fa = fb;
fb.i -> refers to i in the foob class
fa.i -> refers to i in the fooa class
methods are late binded. so:
fa.meth(); -> calls foob meth
meaning that in fooa when you initialize i it calls the foob meth and since foob is not yet constructed i = 0;
after the contruction
fooa.i == 0
foob.i == 2
later
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am still not satisfied with matts reply,thiugh it is right that it will call foob's method.But matt says foob's method is not constructed yet,which i think is wrong because when u do new foob ,the method's and the variables r constructed and that object will have a copy of this class's instance methods and variabls.
if u r clear abt this question kindly mail me at psp74@hotmail.com
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this :
...
fooa f = new foob();
System.out.println(f.i); //A
System.out.println(f.meth()); //B (add this code)
...
the output is :
0
2
Comment A, field "i" is hidden, not overriden :
it mean's that the type of the reference "f" you are using on your code is "fooa" (your code : fooa f = new foob()), so when you try to invoke that field "i" using that reference (f), you will get the field of that "fooa" class;
BUT since you try to initialize the field by invoking meth(), the overriding method will be use (late binding) NOT meth() in class fooa, and you will find that "i" was not initialized yet (order of initialization is not touching foob yet), and that's why zero(0) would be returned.
Comment B, meth() is overriden, not hidden :
If you use a method meth() instead of a field "i" to access the field, the overriden method would be invoked, and 2 would be return.
hope this help
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all..
i still don't understand one thing..
when we say fooa fa=new foob(); fa is already initialized.. isn't it?? sice a superclass's constructor is always called before initializing a subclass.. so where is the ques of i being uninitialized??
------------------
Hima
 
permaculture is largely about replacing oil with people. And one tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!