Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another strange question

 
david hu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Rammohan Meda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tricky Example Guoqiao!

------------------
azaman
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic