This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

another strange question  RSS feed

 
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);
}
}
 
mister krabs
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
 
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
 
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
 
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


 
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
 
Ranch Hand
Posts: 2373
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tricky Example Guoqiao!

------------------
azaman
 
Trust God, but always tether your camel... to this tiny ad.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!