jQuery in Action, 2nd edition*
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

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(){
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(){


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

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

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?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14280

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

Second case:

init-ss-1: null
init-ss-2: 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 8 API documentation
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Strange issue for instance variable.