Solved

close java class

Posted on 2004-03-26
13
212 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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:mayankeagle
mayankeagle earned 25 total points
Comment Utility
>> 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 37

Expert Comment

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

Expert Comment

by:mayankeagle
Comment Utility
Just added the extra line of code ;-)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> Just added the extra line of code ;-)

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

Expert Comment

by:mayankeagle
Comment Utility
Oh.... I thought the "candidate for garbage-collection ". Anyways ;-)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Why a B :-(
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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:mayankeagle
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

743 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

18 Experts available now in Live!

Get 1:1 Help Now