Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default initialization in java

I have a confusion about variable initialization in Java. As I understand, class variables get default initialization while local variables are not initialized by default. However, if I create an array inside a method using the new keyword, it does get initialized by default. Is this true of all objects? Does using the new keyword initialize an object regardless of whether it's a class variable or local variable?

like image 824
B M Avatar asked Mar 01 '13 19:03

B M


1 Answers

Is this true of all objects? Does using the new keyword initialize an object regardless of whether it's a class variable or local variable?

When you use new keyword. it means that you have initialized your Object. doesn't matter if its declared at method level or instance level.

public void method(){
Object obj1;// not initialized
Object obj2 = new Object();//initialized
}
like image 54
PermGenError Avatar answered Oct 27 '22 21:10

PermGenError