wood burning stoves 2.0*
The moose likes Java in General and the fly likes Strange issue for instance variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strange issue for instance variable." Watch "Strange issue for instance variable." New topic
Author

Strange issue for instance variable.

Biswajit Paria
Ranch Hand

Joined: Feb 02, 2003
Posts: 46
Hi All,

Here is the following program, I got strange output for instance variable for the following program.

****************************
abstract class A{
public A(){
init();
}
public abstract void init();
}

public class Test extends A{

//get output for two cases, Why it is different?
//public String ss = "blank";//First Case
public String ss ;//Second case

public void init(){
System.out.println("init-ss-1: "+ss);
ss ="init";
System.out.println("init-ss-2: "+ss);
}

public void test(){

System.out.println("ss****:"+ss);
}

public static void main(String[] args) {
new Test().test();
}
}


Could you please explain, why the output is different in two cases?

Thanks,
Biswait.
Biswajit Paria
Ranch Hand

Joined: Feb 02, 2003
Posts: 46
In more Specific, Why out of 'ss' in test()method is different for two cases?
Thnx.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Note: Please use code tags when you post a piece of Java code, so that the forum software can format it in a readable way.

Ok, I tried your code. The output is this:

First case:

init-ss-1: null
init-ss-2: init
ss****:blank

Second case:

init-ss-1: null
init-ss-2: init
ss****:init

So your question is why in the last line the outputs are different.

To understand why this is, you have to understand the order in which things are initialized when you create a new object. When you create a new object, the superclass part of the object is initialized first. So the constructor of abstract class A is called first. In that constructor, you call init(). Note that at the moment you call init(), the member variables of the subclass part (specifically, the member variable ss in class Test) is not yet initialized - you are calling it while the superclass is initializing, and when the subclass part is not yet initialized.

In init() you set ss to a value "init". Then init() returns to the constructor of the superclass, the superclass constructor returns and then Java initializes the subclass part. So Java then sets ss to the value "blank". You see?

The problem occurs because you are calling init() at the wrong moment - from the superclass constructor.

At the moment I'm working on a piece of software built upon a framework that our client has designed themselves. In this framework we have the exact same problem - the superclass that we're supposed to use for GUI objects also calls an init() method that we're supposed to override in our subclasses. In my opinion it is a design flaw to do something like this. Objects should be initialized in the constructor, because that's what the constructor is for. Inventing special init() methods like this is not a good idea.
[ March 09, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange issue for instance variable.