Initial value of object reference defined as instance variable

Initial value of object reference defined as instance variable

Answer : If you don’t initialize object reference as when define it as instance variable then its value will be “null” . Please see example below:

package com.javahonk.stringtest;

public class ObjectInstanceTest {

    TopClass topClass;

    public static void main(String[] args) {

	ObjectInstanceTest objectInstanceTest = new ObjectInstanceTest();
	System.out.println(objectInstanceTest.topClass);

    }
}

class TopClass {
    private String str;

    public String getStr() {
	return str;
    }

    public void setStr(String str) {
	this.str = str;
    }

}

 

Output:

Initial value of object reference defined as instance variable

 

Leave a Reply

Your email address will not be published. Required fields are marked *