wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please see the following code: 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 "please see the following code:" Watch "please see the following code:" New topic
Author

please see the following code:

David chenjl
Greenhorn

Joined: Jul 05, 2001
Posts: 19
public class Q5 {
byte b=127;

Q5() {
this.methodA();
}

void methodA() {
System.out.println("Value of bb is = " + b );
}

public static void main(String [] args) {
Q5or p = new Q5or();
}
}

class Q5or extends Q5 {
byte b=126;

Q5or() {
System.out.println("Value of bbb = " + b);
}

void methodA() {
System.out.println("Value of b = " + this.b);
}
}
the output is:
Value of b = 0
Value of bbb = 126
i can see that bbb=126, but how b=0? please help me. by the way, will it be in real exam like that situation? I have never meet that using sub class in base calss.
Kamil Dada
Ranch Hand

Joined: Aug 07, 2001
Posts: 43
Originally posted by David chenjl:
public class Q5 {
byte b=127;

Q5() {
this.methodA();
}

void methodA() {
System.out.println("Value of bb is = " + b );
}

public static void main(String [] args) {
Q5or p = new Q5or();
}
}

class Q5or extends Q5 {
byte b=126;

Q5or() {
System.out.println("Value of bbb = " + b);
}

void methodA() {
System.out.println("Value of b = " + this.b);
}
}
the output is:
Value of b = 0
Value of bbb = 126
i can see that bbb=126, but how b=0? please help me. by the way, will it be in real exam like that situation? I have never meet that using sub class in base calss.

Hello,
hmm this is strange, it seems to me that the method methodA in the Q5or class is never even called, so how does it get printed?
I mean, as far as I can see the result should actually be:
Value of b = 126
and that is all (however im most probably wrong!)
and also it looks to me that the Q5 constructor and method methodA is not needed.
How do two lines get printed? Im confused on this.
However if your wondering why the 0 is getting printed from the methodA method (why/how it is getting called/printed I do not know however) is because you are using:
Value of b = this.b
which means that you are actually asking the compiler to print the value of the variable b inside the methodA
which does not exist
try using:
void methodA() {
b = 1;
System.out.println("Value of b = " + this.b);
}

result should be 1 because this.b is calling the variable b from the methodA method and the the variable b from the class.
Hope this wasnt too long
Someone please correct me if im wrong.
And also could some one help me with the code, how do the two lines get printed?
Thank You,
Kamil.

[This message has been edited by Kamil Dada (edited August 13, 2001).]
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
This code is supped to print two lines:
Originally posted by David chenjl:
public class Q5 {
byte b=127;

Q5() {
this.methodA();
}

void methodA() {
System.out.println("Value of bb is = " + b );
}

public static void main(String [] args) {
Q5or p = new Q5or();
}
}

class Q5or extends Q5 {
byte b=126;

Q5or() { //here the constructor in the super class is called, super(), which in turn calls methodA thats why it first prints the statement from methodA. But still I am not sure how come it is getting a value of 0.
System.out.println("Value of bbb = " + b);
}

void methodA() {
System.out.println("Value of b = " + this.b);
}
}
the output is:
Value of b = 0
Value of bbb = 126
i can see that bbb=126, but how b=0? please help me. by the way, will it be in real exam like that situation? I have never meet that using sub class in base calss.


[This message has been edited by Muhammad Farooq (edited August 13, 2001).]


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi Muhammad,
If you know the class initialization sequence, you can understand this better. You can refer JLS Chaper 12.5 for more details.
Just some points about class Initialization:

    Constructor is invoked
    If it has this() as first statement then that constructor is
    called
    If not then super() is called. (not for Object class)When you call super() process is done by using these same steps
    Execute the instance intializers and variables to their value.
    Exeute the rest of the body of the constructor


    Hope this helps,
    If I have done any mistake correct me,
    Vanitha.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Step by step:
1) Since Q5or does not run any constructor of Q5 and Q5or is a child of Q5 run the default constructor of Q5
2) the constructor of Q5 wants to run methodA. methodA has been overridden by Q5or so methodA of Q5or runs.
3) methodA tries to get the value of Q5or.b but b hasn't been set yet in Q5or so it gets the default value of a byte which is 0
4) the consructor for Q5or now runs
5) Q5or gets the value of b which now has been set since the construction process has started.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Thanks Vanitha and Tom, Can you give the specific order of initialization or process by which (both static and non-static)variables, methods, constructor or initializing blocks start to initialize .
--Farooq
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi Muhammad,
Take a look at the following code, it may help.


output is
Static block from Superclass
Subclass static initializer block
Instance block from SuperClass
From superclass Check
From SuperClass Constructor
Subclass Instance Block
From subclass constructor
static blocks and static varibles are initialized only once
so you won't see the first two lines here

Instance block from SuperClass
From superclass Check
From SuperClass Constructor
Subclass Instance Block
From subclass constructor

Hope this helps,
Vanitha.
[This message has been edited by Vanitha Sugumaran (edited August 13, 2001).]
David chenjl
Greenhorn

Joined: Jul 05, 2001
Posts: 19
thank you every one, i have catch it.
thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please see the following code: