how to avoid creation of multiple object for a class

hi,Iam creating object for a class A ,whereever i want to call some method of the class A,Iam creating the object and call that method.... in this way iam creating the object across the multiple classes... i dont knw how to minimise the creation of object...?can anyone tell me the way to minimise the creation of object...

[330 byte] By [83Krisha] at [2007-11-27 9:28:52]
# 1
Smells like a bad design to me.
floundera at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 2
Why do you want to minimize it?
jverda at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 3
coz.... java.lang.OutOfMemoryError exception arises when there is object creation.....
83Krisha at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 4
Use Singleton class......
vrkrajeshrajua at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 5

> coz.... java.lang.OutOfMemoryError exception arises

> when there is object creation.....

Simply creating lots of objects won't cause OOME. It might be that you're holding onto objects you're done with. Or it might be that you legitimately need that many objects, and you just need to increase the memory you give the VM.

Or maybe you do need a singleton.

Can't say without more details.

jverda at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 6
> whereever i want to call some method of the class A,Iam creating the object and call that methodSomething seriously wrong there. Why would you want to call a method of the class unless you already had an instance of it?
ejpa at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 7
What kind of objects that class contains. check it properly or use some profiler tool to find it
harish.suna at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 8

class A

{

int i;

void execute(int j)

{

i=j;

System.out.println(i);

}

}

class B

{

public static void main(String args[])

{

A a=new A();

a.execute(7);

a=null; //nullfied the object here...

}

}

If i want to call the method of the class A from the class B.I need to create the object of the class A in class B.then i invoke the class A method.After the execution of the class A method iam nullfied the class A object.I want to knw if suppose the value initialised in the class A is also nullified or not...?

83Krisha at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 9
You don't "nullify" an object. You "nullify" a reference. The object being referenced is not affected by this. Important distinction to make
georgemca at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 10
oh ok... then how to nullify the object.....?can u tell the way to do that.....
83Krisha at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 11
> oh ok... then how to nullify the object.....?can u> tell the way to do that.....Hmmm...Object obj = <whatever>;obj = null;// nullified
aniseeda at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 12

> oh ok... then how to nullify the object.....?can u

> tell the way to do that.....

The way you were doing it is okay, as long as that is the only place where there is any reference to the object. But, if the class is being initialized and the object then thrown away in rapid succession, the GC may not be able to catch up before the outofmemory error occurs. If you really need to call a single method of a class (and trhen throw the object away) in such rapid succession, you should seriously think about finding a way to make that method static, or to make your class a singleton.

masijade.a at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 13

Simple solution for this is make that method which you have to call freequently as static.

It is one of the advantage of static methods.

since it is static to call that method no object is required for you.

Also only one object will be created in JVM also.

if you are not clear make a mail to me at krosuru@gmail.com

krosurua at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 14

> oh ok... then how to nullify the object.....?can u

> tell the way to do that.....

Don't nullify!

"When programmers are first stung by a problem like this, they tend to overcompensate by

nulling out every object reference as soon as the program is finished with it. This is neither

necessary nor desirable as it clutters up the program unnecessarily and could conceivably

reduce performance. Nulling out object references should be the exception rather than the

norm. The best way to eliminate an obsolete reference is to reuse the variable in which it was

contained or to let it fall out of scope. This occurs naturally if you define each variable in the

narrowest possible scope. It should be noted that on present day JVM

implementations, it is not sufficient merely to exit the block in which a variable is defined;

one must exit the containing method in order for the reference to vanish."

Effective Java Progrmming Language, Joshua Bloch

manuel.leiriaa at 2007-7-12 22:35:27 > top of Java-index,Java Essentials,Java Programming...
# 15

> since it is static to call that method no object is

> required for you.

>

> Also only one object will be created in JVM also.

If the previous message would not have been descriptive enough, I would have certainly sent a mail.

> if you are not clear make a mail to me at

> krosuru@gmail.com

aniseeda at 2007-7-21 23:02:15 > top of Java-index,Java Essentials,Java Programming...