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
Solved

design issue

Posted on 2003-12-10
18
374 Views
Last Modified: 2013-12-03
Hi,

I have an object and I have written a mthod where I clear all the instance variables.

public void clear()
{
this.instanceVariabes = null;
}

I am doing this so that i do  not have to create multiple objects of my class but use the same class.
like this
a.make an object
b. use it
c. clear it
d use it again

instead of
a.make an object
b. use it
c.make another object
d. use it

1. Is it a good design?
2. will i save on instance variable memory.

Anything else pros and cons....

Thx for your help
0
Comment
Question by:applekanna
  • 4
  • 4
  • 3
  • +4
18 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9918226
hard to say without knowing how you are going to use it (and why)
you're (probably) going to save on memory, but you need to ensure that the object isn't being referenced elsewhere when you clear it.
0
 
LVL 3

Author Comment

by:applekanna
ID: 9918244
was just looking for what i should be careful of when do such a thing.
0
 
LVL 92

Expert Comment

by:objects
ID: 9918287
clearing it while its still referenced.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 15

Expert Comment

by:Javatm
ID: 9918291
>> 1. Is it a good design?

      It actually depends on the type of program your doing or creating. There are so many ways you
      can make a design but the design should be handled professionally, look on the samples of the
      designs of some programs which is the same as what your doing for you to decide if what your
      doing is appropriate for the program.

>> 2. will i save on instance variable memory.

     This will happen because your targeting to lose an object on its referrence. Just follow on the
     comment of what objects said about it.

Hope its helps . . .
Javatm
0
 
LVL 3

Expert Comment

by:ShawnCurry
ID: 9918393
1.  If you want only one copy of a variable, declare it as static.
2.  Will it save on memory? It depends how it is used.  If it's something that should be created once and referenced many times, you probably don't want to set it to null - at least put a nice comment block above the line you set it to null... so you can find it quickly if you start getting null pointers...
0
 
LVL 3

Expert Comment

by:ShawnCurry
ID: 9918422
It won't let you declare an entire class as being static, but there's nothing stopping you from declaring every single thing inside static.  The only thing to watch out for here is thread safety..
0
 
LVL 2

Expert Comment

by:Tols
ID: 9918639
There is a nice design schema called singleton. It allows to make such class that there could be only one insctance (object) of this class during runtime.
Its something like :

public class SingletonClass
{
    private SingletonClas singleton = null;
   
    private SingletonClass() ///
   {
       //nitialization
   }
   
   public static SingletonClass getSingletonObject()
   {
        if (singleton==null) singleton = new SingletonClass();
        return singleton;
   }
}

If You wanto to recreate object You may provode reset method in class definition:
//....
   prublic static resetSingletonObject()
   {
        singleton = null;
   }
//....

It's good not to keep references to this object in other classes, use only getSingletonObject() method for referencing to the singleton object. This will make all object would referrence always to current instance.
0
 
LVL 2

Expert Comment

by:Tols
ID: 9918641
correnction - singleton have to be static

private static SingletonClass singleton = null;
0
 
LVL 3

Expert Comment

by:ShawnCurry
ID: 9918665
The reason your singleton needs to be static is because you reference it from a static context "public static SingletonClass getSingletonObject()"
0
 
LVL 2

Expert Comment

by:Tols
ID: 9918697
Yes, yes - it was a slip of the tongue
:-)
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9918977
If you want an example of using a single Object, look into the java.awt.GridBagConstraints object.  The recommended approach to using this is to create a single instance of it, then set the values you require on it.  When it is then passed to the layout manager it is cloned and you can modify your created one further without it affecting the ones that will be used by the layout manager.  Perhaps it's something for you to think about anyway!
0
 
LVL 3

Author Comment

by:applekanna
ID: 9922056
Thx for you help
Actually I have a bean which is basically a record of a database table and and when displaying it on the page
i get all the records as follows

for(//while there are more records in iterator sometimes 200 )
{
  MyBean temp = new MyBean();
  //get my values
  temp = null;
}

But I was told on EE that temp = nul does not help in GC so I thought I will use a clear() method here
and i am not sure if singleton will help
0
 
LVL 3

Expert Comment

by:ShawnCurry
ID: 9922710
The best thing you can do in that case is consult the documention on the bean.  If there is some sort of set() method to set the values you need, you could call the constructor once before you enter the loop.. since you want to recycle the same object, it doesn't make sense to call the constructor (and a clear() method) inside the loop.  Everything I've read on the GC indicates that it runs whenever it feels like it..  Setting the variable to null can help GC clean up, but there's no way to guarantee the memory will be freed (before your program exits, anyway).  The best way to guarantee you don't have irrelevant objects floating around in memory is to never create them in the first place!!!

Hope that helps...

Shawn
0
 
LVL 3

Author Comment

by:applekanna
ID: 9923879
WHat happens  in this case


for(//while there are more records in iterator sometimes 200 )
{
  MyBean temp = (MyBean) iterator.next();
  //get my values
  temp = null;
}

Here you see I am not using any constructor
Thx for ur help
0
 
LVL 92

Expert Comment

by:objects
ID: 9923908
> It allows to make such class that there could be only
> one insctance (object) of this class during runtime.

Not entirely true. There is one static per classloader.

0
 
LVL 92

Expert Comment

by:objects
ID: 9923920
> But I was told on EE that temp = nul does not help in GC

Its not that it doesn't help, its that it is unnecessary.

I don't ythink there is anything to get concerned about, or any design changes needed.
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9925341
I do not get it.
after clearing that instance variable I assume you then proced to give it a value again (by creating an object for it to reference)

So all you have really done is to create a level of indirection between your program and that object. It will make litte or no difference in memory use. It may slow down things a bit, but again the difference will be miniscule.

regards JakobA
0
 
LVL 7

Accepted Solution

by:
grim_toaster earned 125 total points
ID: 9926521
Here's an interesting link about GC, have a look at the section on invisible references.

http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#998394

But in your code:

for() {
  MyBean temp = (MyBean) iterator.next();
  temp = null;
}

The object already exists in memory, so setting your reference to null will be of no benefit (the object will still be referenced by whatever the Iterator is iterating over, i.e. a collection, so GC would not clean it up)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

839 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