Solved

close java class

Posted on 2004-03-26
13
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Suggested Courses

623 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