Solved

close java class

Posted on 2004-03-26
13
213 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 48
jar file executable 12 42
split string containing \r\n in Java 46 38
couple of eclipse 5 23
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now