1

Hi i have a Base class containing one string member as belows :

public class BaseClass 
{
    public String test;
}

Child class extending base class where i wish to initialize the test value.

public class ChildClass extends BaseClass 
{
    public void initialize()
    {
        System.out.println("inside constructor of ChildClass.");
        this.test="stringtest";
    }
}

Test class where i wish to use the value of test variable of base class:

public class TestClass extends BaseClass 
{
    public void test() 
    {
        new ChildClass().initialize();
        System.out.println(this.test);
    }
    public static void main(String[] args) {
        new TestClass().test();
    }
}

Now my above code is printing null inside test class. why so? although i have initialized the test variable in child class? am i going wrong somewhere in java concepts?

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71
  • You're creating a *new instance* of `ChildClass` and initializing that - not *this* instance. Additionally, please try to get used to *not* using public fields, as early as possible... – Jon Skeet Sep 09 '13 at 13:17
  • Hi all thanks for the reply!!! I would like to take this discussion further. what i wanted is that my child class was just suppose to initialize the test variable of base class and then i wanted to use the test variable of base class in my other classes as it is. so how should i go about to achieve this? i don't want to make instances of my child class in test class. TIA – Mrunal Gosar Sep 09 '13 at 13:28
  • It's not really clear what you mean, I'm afraid. It's not obvious why `TestClass` extends `BaseClass` at all. – Jon Skeet Sep 09 '13 at 13:30
  • @Mrunal You seem to be thinking in terms of non-object oriented programming . The only values a subclass could modify would be the ones in its own instance, unless you use `static` members (please don't do that unless you've got a good reason). Another option could be passing in to the subclass a reference to an Object holding the value you want to initialize there. – Xavi López Sep 09 '13 at 13:31
  • @JonSkeet see i have a container which contains member variables(in this case Base class containing test variable). i have set of setter classes which initialize the member variables of container class. i have another set of classes which extends the container class and use the value which was set by the setter classes. that is what i wish to achieve through my above dummy code – Mrunal Gosar Sep 09 '13 at 13:33
  • 1
    @MrunalGosar: Well you've just restated what your dummy code does, but not really *why* you're doing it... – Jon Skeet Sep 09 '13 at 13:38
  • @XaviLópez can you point out some code snippet for your second suggestion? TIA – Mrunal Gosar Sep 09 '13 at 14:00

4 Answers4

1

There are two instances of BaseClass in the example you posted. One is the one instantiated with new ChildClass() and the other one is instantiated by the main() method (TestClass). Each one of them, being a subclass of BaseClass, has its own test member (they are different variables with different values).

Remember that the this keyword always references the instance in which it is used.

In this case, System.out.println(this.test); is accessing the test property of the TestClass instance created in the main method.

You need to access the test property of the other instance. You could do so by keeping a reference to the ChildClass instance and accessing the test property afterwards:

    ChildClass instance = new ChildClass().initialize();
    System.out.println(instance.test);

You might find the following Java Tutorials page useful: Using the this Keyword.

Also take into account that TestClass doesn't need at all to extend BaseClass. You could keep accessing instance.test because it is a public member, but you should consider making the field private and provide getter and setter methods. See the following question for relevant information on this: Why use getters and setters?

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • can you post a code snippet. where the base class has the test variable. child class has the setter of this test variable. and test class has the getter of this test variable. please note that once the test variable is set then i should be able to access the same value in all other test class which are extending the base class. how can i achieve it. i am trying it but not able to make out the design as to how to keep things? – Mrunal Gosar Sep 09 '13 at 14:35
  • @Mrunal Is there any specific reason to take the inheritance approach? Take into account that every instance of a class or subclass will have its own values for the variables. Mayber you'd be better with [composition](http://stackoverflow.com/q/49002/851811) instead. – Xavi López Sep 10 '13 at 11:18
1

the problem is that you create new ChildClass but you aren't setting it in a variable. then you print this.test which is never set.

when you are in test method you are in TestClass instance:

enter image description here

you are creating and setting a ChildClass class

enter image description here

but then you are printing the TestClass test member

enter image description here

if you just want to create ChildClass and use it do

public class TestClass
{
    public void test() 
    {
        ChildClass cls =new ChildClass().initialize();
        System.out.println(cls.test);
    }
    public static void main(String[] args) {
        new TestClass().test();
    }
}

or if you want to extend ChildClass do

public class TestClass extends BaseClass 
{
    public void test() 
    {
        initialize();
        System.out.println(this.test);
    }
    public static void main(String[] args) {
        new TestClass().test();
    }
}
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
1

new ChildClass() and new TestClass() are 2 different objects even if they are extending common BaseClass.

Having a common BaseClass as super class doesn't mean that it shares the non static fields of it, with all the instances of its different subclasses

This would have worked if test was static (shared class field) in BaseClass

sanbhat
  • 17,522
  • 6
  • 48
  • 64
0

You need a instance of Child Class

ChildClass cls =new ChildClass().initialize();
System.out.println(cls.test);


this is referring to the Test Class instance.
JNL
  • 4,683
  • 18
  • 29