aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another strange question 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 "another strange question" Watch "another strange question" New topic
Author

another strange question

david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143
why output is :
Prints Value of b = 0 and Value of b is = 126.
===================
class Process {
byte b=127;

Process() {
this.methodA();
}

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

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

class Processor extends Process {
byte b=126;

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

void methodA() {
System.out.println("Value of b = " + this.b);
}
}
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Change the program with this and view the results. this might give away the answer:

When you run methoda() from the Process constructor, it actually runs methoda() of the Processor class because of polymorphism. This causes it to access b from Processor which hasn't been set with its starting value yet when the Process constructor runs. (Remember that the Process constructor runs before the Processor constructor).
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums


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

Joined: Jul 15, 2001
Posts: 112
I am sorry but i think i am confused.
Is it not this order a class is loaded,compiled and run?
Static variables are first loaded followed by instance variables and then constructors.
I fthat is the case then if i change byte b=127 in the Process class to static byte=127,then it should load first and display 127 ....right???
i am confused
plz help!!!
Thanx
Rajani
Alex Sbityakov
Ranch Hand

Joined: Jul 23, 2001
Posts: 49
Since the method of class processor is called, it looks for the b in its own class and will still print out 0. The fact that you say static b = 127 in the parent class has no bearing on the child class.
But, if you say static b = 126 in the child class, then it will print 126,126 instead of 0,126. In this example, first the static initializers are executed (we have none), superclass constructor is called, initializers are executed, rest of constructor is run.
So, the superclass runs and calls a child method before b is initialized and the program prints out 0.
Correct me if I'm wrong,
Alex
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
Hi, I gave an example for the class and object initialisation sequence as follows:

The result is as follows:

which mean Alex's point of view is right.
Hope it helps.
Guoqiao
Originally posted by Alex Sbityakov:
Since the method of class processor is called, it looks for the b in its own class and will still print out 0. The fact that you say static b = 127 in the parent class has no bearing on the child class.
But, if you say static b = 126 in the child class, then it will print 126,126 instead of 0,126. In this example, first the static initializers are executed (we have none), superclass constructor is called, initializers are executed, rest of constructor is run.
So, the superclass runs and calls a child method before b is initialized and the program prints out 0.
Correct me if I'm wrong,
Alex


Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Rammohan Meda
Greenhorn

Joined: Jul 18, 2001
Posts: 4
Hi
this will be explained by the principle of "Variables will be shadowed and Methods will be overridden". In above problem object of Processor (Processor p = new Processor() is being created at the main() method, this executes the constructor of the Process class (because Processor extends Process), Process constructor calling this.methodA() then the control will goes to the child calss ie. Process as per the above stated principle. when the control comes to the Processor methodA(), the byte b is not yet initialized, hence it will print 0 (zero), next the control flows down which intializes the byte b and executes the Processor constructor, which prints b=126, the output won't change if you define b as static at calss Process.
All the best.
Rammohan
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370


Tricky Example Guoqiao!

------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another strange question