Joeblackdev
Joeblackdev

Reputation: 7327

When is an object declared and initialized in a local reference created in Java?

If I have the following code:

public class Foo {

   private Object obj = new Object();

   public void bar() {
      final Object obj2 = new Object();
   }
}

Upvotes: 1

Views: 94

Answers (1)

djhaskin987
djhaskin987

Reputation: 10077

That is the case. Objects in scope of a method will be only instantiated when the method is called, while members of a class so declared will be instantiated when the object is constructed.

Upvotes: 3

Related Questions