Solved

close java class

Posted on 2004-03-26
13
215 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:Kennywen
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 10685410
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
 

Author Comment

by:Kennywen
ID: 10685438
that mean i will end by itself?? is there any way for us to end the new operator?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10685457
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
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.

 
LVL 37

Accepted Solution

by:
zzynx earned 25 total points
ID: 10685489
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 10685549
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
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 25 total points
ID: 10685609
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 10685620
Now, *you* are "repeating" *me*. Aren't you? ;)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10685631
Just added the extra line of code ;-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10685664
>> Just added the extra line of code ;-)

mayankeagle, in fact I was talking to krakatoa :)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10685714
Oh.... I thought the "candidate for garbage-collection ". Anyways ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10720795
Why a B :-(
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10721396
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10721433
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question