• 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Strange issue for instance variable.

Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In more Specific, Why out of 'ss' in test()method is different for two cases?
Java Cowboy
Posts: 16084
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic