Solved

close java class

Posted on 2004-03-26
13
214 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 196
VB Script to add site to Java Exception List 4 79
Java Loop 6 62
Is there a simpler dropbox system? 10 24
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)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

831 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