Win a copy of Learning Regular Expressions this week in the General Computing 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

practice question: predict the result  RSS feed

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The answer to the following problem should be 2. The print method is called by the following code " ic.bMethod();" which belongs to class Inner. The bMethod calls the method print() from class Test which prints the variable i of class Test which has been given the value 2.
Hence, The solution is 2.
Kind Regards
Muzz
NOTHING WORTH WHILE WILL EVER BE ACHIEVED WITHOUT DEEP THOUGHT AND HARDWORK - J.R.D TATA
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is right Muzzamil. Can you guess what will be the out if inner class does not extend outer class?
[ September 21, 2003: Message edited by: Barkat Mardhani ]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It should print only 2 even if the class doesn't extend Test, since i is 2 in the method context. It will print 3 only if the print() method is defined(overridden if the class extends Test) in the inner class....
Thanks,
Uma..
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, so try to change print() method in Test as,
void print() {
System.out.println(this.getClass().getName());
System.out.println(this.i);
}
You will see that first print says its Inner type of object but when we do this.i it prints 2 which is Test object's variable. WHY? Inner has two copies of variable i but when we do this.i , I was expecting output to be 3 not 2.
Regards
Maulin
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I was expecting output to be 3 not 2.


In a field access expression the compile type of the variable is what determines the field accessed. The compile type of this is the class that encloses it. In this example is Test, though at runtime this is pointing an instance of type Inner.
[ September 22, 2003: Message edited by: Jose Botella ]
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jose
Thanks. That explains it.
Regards
Maulin
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very nice question
 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this one is nicer...

[ September 24, 2003: Message edited by: mike acre ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!