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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With