File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes reusability of object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reusability of object" Watch "reusability of object" New topic
Author

reusability of object

rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
hi guys..i donot know if it is a right place to ask question but since i am using this concept in my servlet so i put this question here.

i have created a simple javaBean having some getters and setters.

The javaBean class is CapacityReqdBean

now in my servlet , in a function i am doing something like this





but what i want to achieve is not to create so many objects ..instead creating a single object and reusing it.
is there any way..

i tried different thins like....




but it gives an error saying that capacityReqdBeanObj object is already defined

i also tried




but in this case it gives a null pointer exception...
can any one please help me...
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I don't really get the question. Do you want to reduce the number of objects that land on the heap or the number of reference variables ?

if it is the reference variable, you need to reassign it a new object after you set it to null (you can omit the null too).



JDBCSupport - An easy to use, light-weight JDBC framework -
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
I dun think that is what Rammie is trying at - she wants to minimize the number of objects on the heap.

In the first case - it is not valid to declare reference variable with the same name in the same scope. Even of different types. (a simple programming concept)

In the second - we are assigning the reference to null (and null is just null it does not mean resetting the fields of the instance in question) -- (again a simple java concept)

Now here what we can do is clone() the same instance and then set its fields. But for that the we will have to make that class cloneable(a different story).


Programmer Analyst || J2EE web development/design
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You won't get far with this approach.

This is what you have now:

- A multiple number of objects that all have different state which you add to a list.

If you change your design to only use one object instance, then whatever change of state you make will affect all the rest that you have worked with so far, because you act on the same instance.

This is what it looks like:



Assuming list is an ArrayList, you will have two identical instances with a name of both Michael. Considering the code you showed above, this is not what you want.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

I agree with Sebastian.
In your second one where you are getting the error you are putting this:


you only need to do this the first time, the others should be:


This is why you are getting the error as you already have an object with this type and name declared.

Sean


I love this place!
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
Sebastian Janisch wrote:




Being aware of it Rammie is trying to point the reference to some new and fresh memory location on heap. Though it was not done the way Sean has just said. But still we are not avoiding creating new heap object - Cloning seems to be only way round
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
What does clone have to do with anything ? When cloning you do create a new object instance and copy (clone) the state of object A to object B.
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
Hi every body ...thank you for all your responses.

see i really don't have a very deep knowledge of cloning and all ......but what Sebastian Janisch has listed is suiting my case and it is working fine.
basically i wanted to have a minimum number of objects on heap.

thanks guys.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
rammie singh wrote:Hi every body ...thank you for all your responses.

see i really don't have a very deep knowledge of cloning and all ......but what Sebastian Janisch has listed is suiting my case and it is working fine.
basically i wanted to have a minimum number of objects on heap.

thanks guys.


I hope you don't refer to the PersonBean example above, because this is totally not what you want !
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



basically i wanted to have a minimum number of objects on heap.


There is really *no* difference in number of objects created comparing to your first example.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
In general, there is no reason for minimizing the number of bean instances. A JavaBean is a container for data representing a certain state. They are intended to show up in multiples. You would not treat 2 Persons as the same person, so neither do you try to merge two PersonBeans into one.
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
no Sebastian Janisch

i have tried this part




and it is working fine..thanks.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
yap that makes sense ...
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
rammie singh wrote:Hi every body ...thank you for all your responses.
.
.
basically i wanted to have a minimum number of objects on heap.

But the way you are doing - will create the same number of objects as if you were doing

It is just that you are not being bogged down by so many new references. Though that will be no way reducing the load on memory or will not do any other kind of optimizations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reusability of object
 
Similar Threads
maximum open cursors exceeded
Showing Date if no value
Keeping user preferences for that page after periodic page refresh for multiple browsers / PC
nested ResultSets
Null is not an instanceof Object