close java class

i want to know how to close a java class?

e.g.
MyClass newClass = new MyClass();

how to close the above class?

When the class will be close if java cannot close it?

thanks
KennywenAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
zzynxConnect With a Mentor Software engineerCommented:
I guess you mean how to be sure the created instance of a class is removed from memory.

Well, that's the work of Java's garbage collector (GC)
Whenever an object is no longer referenced, it's a *candidate* to be removed from memory.
This doesn't mean it is removed. But that up to the GC to decide.

The only thing *you* have to take care of (to avoid memory leaks) is:
make sure that all references to an object you don't need no longer are gone.
0
 
krakatoaCommented:
public class Main {
    public static void main(String[] args) {

new Main();
         
    }
}


This is the simplest class you can have almost. It will just be loaded and end.

If its a thread,. it will end when run() is finished.
0
 
KennywenAuthor Commented:
that mean i will end by itself?? is there any way for us to end the new operator?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
krakatoaCommented:
Not quite sure I follow what you mean by end the new operator. Objects in Java - and that means just about everything, cease to exist when they are not referenced by anythin else, so when they go out of  scope, they die.
0
 
krakatoaCommented:
In languages such as C++ there aer *de*structors, which are the opposite of constructors. In Java, you dont need that, because when an object is no longer pointed to (referenced) by any other object, then it is removed from memory by the garbage collector - System.gc() - automatically.
0
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
>> MyClass newClass = new MyClass();
>> how to close the above class?

You can set: newClass = null ;

- the MyClass object allocated by the new operator now becomes a candidate for garbage-collection (as long as there are no more references to it). The JVM will soon garbage-collect it (you cannot force it).
0
 
zzynxSoftware engineerCommented:
Now, *you* are "repeating" *me*. Aren't you? ;)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Just added the extra line of code ;-)
0
 
zzynxSoftware engineerCommented:
>> Just added the extra line of code ;-)

mayankeagle, in fact I was talking to krakatoa :)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Oh.... I thought the "candidate for garbage-collection ". Anyways ;-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Why a B :-(
0
 
zzynxSoftware engineerCommented:
Yeah, I also wonder...
Sometimes people think that the grades relate for the difficulty of the question.
But that isn't the case. If the answer is good an to the point it deserves an A.

Anyway, thanks for accepting.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yeah, it also makes no difference to the questioner as they only spend as many question-points as they allocated to the question. Some closing help here:

http://www.experts-exchange.com/help/closing.jsp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.