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.

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...

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.

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

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!

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!!!

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.

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

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!

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…

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…