When is it necessary to use the new
keyword in Java. I know you are supposed to use it when you create an instance of an object like this:
TextView textView = new TextView(this);
Sometimes in code I notice that new
isn't used and I get confused..
In this line of code:
AssetManager assetManager = getAssets();
Why isn't an instance of the AssetManager created like this:
AssetManager assetManager = new AssetManager();
then it is set equal to getAssests()?
When should new
be used?
Thanks!
In Java,
new MyClass()
creates a new instance ofMyClass
and returns a reference to it.Meanwhile, the declaration:
defines a variable
foo
that can store a reference to an instance ofMyClass
, or the special valuenull
that indicates the absence of such a reference. If you don't assing any reference tofoo
, its default value will benull
.You can certainly combine these things, e.g. like this:
or, equivalently, like this:
But you don't have to create a new instance every time you assign something to a reference variable. You could just as well do e.g. this:
or even:
Note that in Java, as opposed to some other languages like C++, you can never assign (a copy of) an actual object into a variable. Rather, Java objects are always accessed via references to them. When someone speaks of "assigning an object to a variable" or "passing an object as a parameter" or "returning an object from a method" in Java, what they always actually mean is assigning or passing or returning a reference to an object.
Methods that your code calls can (and often do) also return references to objects. For example, if your class had a method like this:
you could call it and save the reference it returns into a variable like this:
or like this:
We can tell from the method declaration that
getSomeObject()
will always return a reference to aMyClass
instance (ornull
), but not where that instance actually comes from. That depends entirely on what the code inside thegetSomeObject()
method does. The code might always create a newMyClass
instance withnew MyClass()
every time it's called, and return a reference to it, or it might always return a reference to the same object. Or, of course, it could sometimes return a reference to a new object, and sometimes to a previously created one.Note that, if you assign a new value to a variable, whatever the variable contained before will be forgotten. If the forgotten value happens to be the last reference to some object, then that object itself will be destroyed by Java's garbage collector.
So, for example, while it's certainly possible to do something like this:
that normally makes no sense, since you'd be creating a new
MyClass
instance just to immediately throw away your only reference to it and replace it with something else. In effect, the code above is exactly equivalent to:The only time that could even remotely make sense would be if you wanted to create a new
MyClass
instance and immediately let it be garbage collected, e.g. because theMyClass
constructor had some side effect that you wanted to trigger. But since it's usually considered bad style for constructors to have any non-trivial side effects (or, more generally, to do anything besides setting up the new object), you shouldn't normally have any excuse for writing such code.In java you always have to use
new
to instantiate objects (well, almost always). WithgetAssests()
you retrieve an already created one. I guess your question comes from c++ wherenew
allocates dynamic memory, but since java has only dynamic objects, new is always needed.