Solved

design issue

Posted on 2003-12-10
18
370 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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…

762 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

21 Experts available now in Live!

Get 1:1 Help Now