Constructor call question
I have a chunk of code in class Aquarium that looks like this:
Code:
public static void main(String[] args)
{
new Aquarium();
}
I know that "new Aquarium()" is used to create a new Aquarium object, but what is the scope of this new object? Why would you want to do this?
Thanks.
Re: Constructor call question
If you don't want to hold a reference to the object, do that.
for example, if you place all code necessary to work with a JFrame in it's constructor. You want need to hold a reference to it in the main program
Re: Constructor call question
So when this constructor is called, is this object immediately destroyed or does it hang around until main is completed?
Re: Constructor call question
This depends on what type is the object...
Re: Constructor call question
Quote:
This depends on what type is the object...
I am sorry, but I don't quite know what you mean. I am sort of new to Java.
Re: Constructor call question
Every object created with new hangs around at least until there are no more references to it, and then some (until the GC runs and kills it). In other words, object lifetime in Java is non-deterministic: you cannot predict it exactly.