This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor's call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor Watch "constructor New topic
Author

constructor's call

Suhita Reddy
Ranch Hand

Joined: Jun 09, 2006
Posts: 60
hi,

wht's the o/p of this?can any one explain the flow of pgm?



// file TestA.java

package a;

public class TestA {
protected int i = 1;
}// file TestB.java


package b;

import a.TestA;

public class TestB extends TestA {

static int i = 2;

public static void main(String[] args){
TestB testB = new TestB();
testB.test();
TestC testC = new TestC();
testC.test();
}

public void test(){
System.out.println("TestB i=" + i);
}

static class TestC{
static int i = 3;
public void test(){
System.out.println("TestC i=" + i);
}
}

}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The output is pretty straightforward.

The answer really doesn't depend on constructors.

In both TestB, TestC, and TestA, default constructors are created.

The method in TestB prints out 2, and the method in TestC prints out 3.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
The question tests the concept of "variable hiding and shadowing".
 
 
subject: constructor's call