aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q on variable Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q on variable Initialization" Watch "Q on variable Initialization" New topic
Author

Q on variable Initialization

Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Hi All,

Output is = STP
My Query is , at //L1 we are creating a new Inastance of OuterTest with 'id' initialized with "STP". In //L2 we are creating a instance of InnerTest, which invokes the default constructor of 'OuterTest' within which 'id' is re-initialized to "Default". Then why is the output "STP".
help me understand whats going on i am
Reshma


SCJP 1.4, SCWCD 1.4
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
This is a trick question. The trick is the fact that the inner class extends the outer class. As a result, the inner class inherits the members of the outer class. Therefore, there are two variables named "id" associated with the inner class. One of the two is inherited. The second of the two is the one that is a member of the enclosing class. The "id" variable that is inherited has the "default" value. The "id" variable that is a member of the enclosing class has the value "STP".
[ March 27, 2003: Message edited by: Dan Chisholm ]

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Thanks a ton dan, your explanation was extremly helpful. If i do well in my SCJP, part of the credit goes to you. Thanks again for helping me understand JAVA better.
i still have a question to be answered, here it goes.......

Oput is 0.
My query is, in the Parent constructor when we say 'this.i'(//Line 1), are we not meaning to say the current objet's(that is 'c') variable 'i'.
I am very mush confused with the use of keyword 'this' in parent class, as to which resources do they point to. Can u please enlighten me on this.
Thanks
reshma
[ March 28, 2003: Message edited by: Reshma Pai ]
Arvind Dua
Greenhorn

Joined: Mar 28, 2003
Posts: 5
As per my understanding goes, when a subclass object is created even the superclass constructor is called but in this sequence the collection of variables for the two is different. That is to say when a superclass constructor gets called from within a subclass, a new object is created that holds the variables for the superclass.
So in the code snippet given by you, there will be two instances of variable i, and we are printing the variable i associated with the subclass whose value is not set. Hence the result 0.
Hope this helps. Please correct me if i am wrong.
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Arvind,
I agree to what you say i,e. when in the code used 'c.i' its the value of i which is defined in 'C'. This is an example of data hiding.
My Query here is with the usage of keyword 'this' in parent class constructor. They behave differently with 'variables' & 'method invocation' ...
Considering this example...

output for the above code is = Child = 0
Is the usage of 'this' regarding 'variable' and 'methods' are different.

reshma
Falk Lucius
Greenhorn

Joined: Mar 27, 2003
Posts: 7
Reshma,
you have got it. By redefining the 'i' variable, the Child class is said to be 'hiding' the 'i' variable of the Parent class which it otherwise would inherit. Thus, references to 'i' of a Child instance always point to the 'i' variable of the Child class instead of the Parent class. In order to address the Parent's 'i' variable, you would have to insert the statement 'this.i = super.i;' into the Child class constructor.
The 'this' prefix in the Parent class therefore refers to an instance of the Parent class only, not to an instance of the Child subclass. Not even invoking the superclass constructor would have any effect on that.
And remember: The superclass constructor is not called automatically because the Child class is not instantiated by a default constructor, but by a no-arg constructor which is a different thing. In this case, in order to invoke the superclass constructor, the no-arg constructor must have the 'super();' statement at the beginning of its definition which is here completely empty.
Take care,


Falk J. Lucius<br />SCPJ2 1.4
Falk Lucius
Greenhorn

Joined: Mar 27, 2003
Posts: 7
Reshma,
I have to correct myself, sorry. The superclass constructor is invoked automatically during the intantiation of a Child object. So, please erase the last paragraph of my previous reply.
Thanks,
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202

In this case, in order to invoke the superclass constructor, the no-arg constructor must have the 'super();' statement at the beginning of its definition which is here completely empty.

This is not right.. refer the 8.6.5 section of JLS. I will paste below an abstract of what it mentions.

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

Hope this helps you
reshma
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Falk,
i had not seen your second post when i replied to your first post.
Reshma
Lih Chang
Greenhorn

Joined: Mar 21, 2003
Posts: 19
Please take a look at the following codes, hope it will give you some hints.
class Parent{
int i=10;
Parent(){
this.showI();
}
public void showI(){
System.out.println("Parent = " + this.i);
}
}
class Child extends Parent{
int i;
Child(){
}
public void showI(){
//the following will access this Child object (pointed to by this)
System.out.println("Child = " + this.i);
//the following will access Parent object (pointed to by super)
System.out.println("Child = " + super.i);
}
public static void main(String[] args){
//the reason that the Child showI() is called in the Parent's Ctor
//is because showI() is dynamically bound.
//In Java, a static method is early bound, other instance methods are late binding.
//In other words, the object that invokes the method will depend on the real object type at the run time.
Child c = new Child(); //the real type for c at run time is type of Child
Parent d = new Child(); //same as above
Parent e = new Parent(); //will invoke showI() in the Parent
}
}
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Thanks to all for helping me understand the topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q on variable Initialization