Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reflection - how to get instance of a class? 2

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
CH
Hi,

I try to find out if there's already an instance of a class available. If not, I would need to create one, if yes, I want to retrieve a value from it's public field.

There's a nice example on sun.com. But how do I retrieve the instance? In the field.get(OBJECT) method the ask me to pass the instance.

Thanks
 
If you haven't got a reference to an instance, you haven't got an instance. You can create a new instance using the newInstance method of the class.

Tim
 
Or course there is an instance. Imaging a user presses a button. That button creates an instance of a class (so there's RAM allocated for this object). No, how do you know how many times that user alredy instantiated the object??? Well, hard to say, isn't it. That's what i want to find out. I want to find out how many instances of a class are available. Because if there is one, I dont' want to create it again. Do I make sense? Clear what I'm trying to do?

Thanks for your help.
 
I try to find out if there's already an instance of a class available

If there *was* an instance available which you have created at some point, you'd have to have a reference to it somewhere. If you didn't the garbage collector would swallow it. So what I mean is, if you have instances you want to check like this, then you'd keep their references in some kind of structure which you could then query to find instances. A kind of object pool.

Code:
how do you know how many times that user alredy instantiated the object???

How about a static counter which is incremented by the class constructor? Problem comes with objects which are dereferenced. If you were to put a decrementor in a finalize() method, it wouldn't update the count until the GC chomped the instance.


Tim
 
Was an instance created?
Code:
 public class Foo
{
	private static boolean instanceCreated = false;
		
	public Foo 
	{
		instanceCreated = true;
	}
	public static boolean wasInstanceCreated () { return instanceCreated; }
}
how many?
Code:
public class Foo
{
	private static int instanceCounter = 0;
		
	public Foo 
	{
		++instanceCounter;
	}
	
	public static int getInstanceCount () { return instanceCounter; }
}
get it:
Code:
public class Foo
{
	private static Foo instance = null;
		
	private Foo 	{	}
	
	public static Foo getInstance () 
	{ 
		if (instance == null)
			instance = new Foo ();
		return instance; 
	}
}
That's called the singleton-pattern, and isn't implemented treadsafe in the example above.
Try to avoid singletons. Perhaps later you will need more than one Instance, or the singletoness of this class isn't enough.
It is only single to a classloader. You can start multiple instances of the program on the same machine i.E.

don't visit my homepage:
 
singletone pattern - great. I'll bend my demands to fit that pattern.

thanks a lot, guys
 
russland

The Singleton pattern is great if you really need a singleton. Take heed of Stefan's warnings. Because the Singleton pattern is one of the easiest to understand, everyone seems to over-use it. Even where it's not really required.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top