Which class is super class of every class

What value String automatically initialized

Answer : By default value will be initialized “null” . If you define variable in different class you will get string value “null” and also note that if you define in same class and try to use it then you will see compile time exception : The local variable string may not have been initialized . Please see example below:

package com.javahonk.stringtest;

public class StringTest {

    public static void main(String[] args) {

	//Example 1 from different class
	TopClass topClass=new TopClass();
	System.out.println(topClass.getStr());

	//Example 2 from same class
	//if you try to use local string varialbe compiler
	// will complain: 
	//The local variable string may not have been initialized
	String string;
	System.out.println(string);//Exception you have to initialize it

    }
}

class TopClass {
    private String str;

    public String getStr() {
	return str;
    }

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

}

 

Leave a Reply

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