• 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

from Khalid Mughal pg no:301

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Nesting{
public static void main(String args[]){
B.C obj=new B().new C();
}
}
class A{
int val;
A(int v){val=v;}
}
class B extends A{
int val=1;
B(){super(2);}
class C extends A{
int val=3;
C(){
super(4);
System.out.println(B.this.val);
System.out.println(C.this.val);
System.out.println(super.val);
}
}
}

Could you please explain how the ans got as 1,3,4 inthis order?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here the rule is that before doing anything in the constructor, the constructor of super class will be called. The other rule is that after all the constructor call over, the instance variable initialization happens.
Here I am giving you the sequence.

1.First new B(), this calls the B class constructor, this calls the super(2) first.
2.This super(2) assign the variable val as 2. This is A class val variable.
3.Now the instance variable of B class assignment happens, this assign the val variable of B class with 1
4.At this point, the B class has two val variable access, one with class A value(2) and other with class B value(1).
5.After this on the B class object we are calling the nested class C class object.
6.In the constructor of C class we are calling first super(4), as the super class is A, the val variable for A is again assigned to value 4.The B class val is still 1 and B class�s A class val value is still 2 and still accessible.
7.Finally we assign the val variable in C class as 3
8.So first we are printing the B class variable 1
9.Then C class local variable 3
10.Then we have used the super which is A class variable for C, that is 4.
11.Add one other statement
a.System.out.println(B.super.val);

It will give that hidden 2 value for B class�s A class val variable.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a good explanation Manoj Macwan
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manoj Macwan:
11.Add one other statement
a.System.out.println(B.super.val);

It will give that hidden 2 value for B class�s A class val variable.



Thats a good observation and a point to be noted!
 
Manoj Macwan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raghavan,
Still new to this forum, and learning a lot from this forum.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manoj Macwan:
Thanks Raghavan,
Still new to this forum, and learning a lot from this forum.



Welcome Manoj.

Thats how every one does and thats what this forum is for!

Keep going!
[ August 10, 2007: Message edited by: Raghavan Muthu ]
 
sukhavasi vasavi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Manoj.You explained very well.
reply
    Bookmark Topic Watch Topic
  • New Topic