Is there any way to get a already created instance of a object?

Is there any way to get a already created instance of a object not a new intance? Is there any way to get the last time that a object instance state has been changed? Is there any way to get a clue that a instance is being used?
[249 byte] By [raviranjana] at [2007-10-3 6:25:01]
# 1

> Is there any way to get a already created instance of

> a object not a new intance?

Only if you have a reference to a previously created object. Otherwise you have to create a new one. If you want to keep an old one around for use later, you have to keep track of it yourself.

> Is there any way to get the last time that a object

> instance state has been changed?

That's completely dependent on what "changed" means to a particular object.

> Is there any way to get a clue that a instance is

> being used?

I don't know what you mean by "being used."

hunter9000a at 2007-7-15 1:10:57 > top of Java-index,Java Essentials,Java Programming...
# 2

> Is there any way to get a already created instance of

> a object not a new intance?

Object existing = new Object;

Object reference = exisitng;

> Is there any way to get the last time that a object

> instance state has been changed?

yes. If the object itself logs those changes and can provide a time stamp when asked. In other words: if you implement it that way, yes.

> Is there any way to get a clue that a instance is being used?

Log all method calls to the object. And if it's not used, it'll only exist until GC runs.

CeciNEstPasUnProgrammeura at 2007-7-15 1:10:57 > top of Java-index,Java Essentials,Java Programming...
# 3

> Is there any way to get a already created instance of

> a object not a new intance?

factory, singleton?

> Is there any way to get the last time that a object

> instance state has been changed?

track itself eg onChange method

> Is there any way to get a clue that a instance is

> being used?

observable?

mchan0a at 2007-7-15 1:10:57 > top of Java-index,Java Essentials,Java Programming...
# 4

> Is there any way to get a already created instance of

> a object not a new intance?

It sounds like you want a "singleton". Here is a simple example

public class SingletonObject

private static SingletonObject ref;

{

private SingletonObject()

{

// no code req'd, put what is needed

}

public static SingletonObject getSingletonObject()

{

if (ref == null)

// it's ok, we can call this constructor

ref = new SingletonObject();

return ref;

}

}

SingletonObject.getSingletonObject() will return you the single instance of SingletonObject. You can only get an instance throught this factory method. new SingletonObject() won't work since the constructor is private.

> Is there any way to get the last time that a object

> instance state has been changed?

> Is there any way to get a clue that a instance is

> being used?

Stick methods in SingletonObject and getSingletonObject to keep trace of what you want to.

For a full explanation see

http://www.javacoffeebreak.com/articles/designpatterns/

BillBlalocka at 2007-7-15 1:10:57 > top of Java-index,Java Essentials,Java Programming...
# 5

> > Is there any way to get a already created instance

> of

> > a object not a new intance?

>

> It sounds like you want a "singleton". Here is a

> simple example

>

> > public class SingletonObject

>private static SingletonObject ref;

>private SingletonObject()

> {

>// no code req'd, put what is needed

>

> public static SingletonObject

> getSingletonObject()

>{

>if (ref == null)

>// it's ok, we can call this constructor

>ref = new SingletonObject();

>return ref;

> }

>

> }

>

Simpler example:

public final class SingletonObject {

private static SingletonObject ref = new SingletonObject();

private SingletonObject() {

// ...

}

public static Singleton getInstance() {

return ref;

}

}

Your example is missing the thread safety you wrote about in the other thread, btw.

CeciNEstPasUnProgrammeura at 2007-7-15 1:10:57 > top of Java-index,Java Essentials,Java Programming...
# 6

> Simpler example:

>

> public final class SingletonObject {

> private static SingletonObject ref = new

> SingletonObject();

>

>private SingletonObject() {

>// ...

> }

>

>public static Singleton getInstance() {

>return ref;

> }

> }

>

> Your example is missing the thread safety you wrote

> about in the other thread, btw.

1. I agree that your suggestion is simplier and it is the method I generally use if the Singleton is instantiated with the default constructor.

I copied the initial snippet from the link, duely attributing the author, without modification so the originator of this thread could look up the reference and not be confused by the difference between my post and the link.

The example as cited has the benefit of being more readable. How the constructor is called the first time is not obvious in the simplier example.

2. The first article begins with a simple singleton and expands upon the example. I posted the simplest complete singleton in the link. The thread safety issue is addressed later in the link.

Generally I agree Singletons are overused but I feel that they are a good place to start and tha design pattern is very well documented.

Have a good one!

BillBlalocka at 2007-7-15 1:10:57 > top of Java-index,Java Essentials,Java Programming...