design issue

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
LVL 3
applekannaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
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
applekannaAuthor Commented:
was just looking for what i should be careful of when do such a thing.
0
objectsCommented:
clearing it while its still referenced.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

JavatmCommented:
>> 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
ShawnCurryCommented:
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
ShawnCurryCommented:
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
TolsCommented:
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
TolsCommented:
correnction - singleton have to be static

private static SingletonClass singleton = null;
0
ShawnCurryCommented:
The reason your singleton needs to be static is because you reference it from a static context "public static SingletonClass getSingletonObject()"
0
TolsCommented:
Yes, yes - it was a slip of the tongue
:-)
0
grim_toasterCommented:
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
applekannaAuthor Commented:
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
ShawnCurryCommented:
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
applekannaAuthor Commented:
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
objectsCommented:
> 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
objectsCommented:
> 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
JakobACommented:
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
grim_toasterCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.