1

For example I have a class like this:

public class StaticObjectReference{
     private static StaticObjectReference instance;
     private Vector queue;

     public static StaticObjectReference getInstance(){
         if(instance == null){
              instance = new StaticObjectReference();
         }

         return instance;
     }

     public Vector getQueue(){
         queue = new Vector();
         return queue;
     }
}

And these next two classes called the StaticObjectReference class.

public class CallerOne{
    Vector queue1;

    public void callObjectInstance1(){
         queue1 = StaticObjectReference.getInstance().getQueue();
    }
}

class CallerTwo{
    Vector queue2;

    public void callObjectInstance2(){
         queue2 = StaticObjectReference.getInstance().getQueue();
    }
}

Is the queue1 in the class CallerOne the same instance queue2 in the class CallerTwo?

Jj Tuibeo
  • 773
  • 4
  • 18
  • 2
    You are implementing a singleton in an unsafe way and if you're talking about multiple threads you can have two instances of your `StaticObjectReference` created in an edge case. See: http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – Brian Roach Jan 10 '13 at 02:48
  • @Brian Roach - so you're saying that if I have a multiple threads, I can also have many instances of my "queue"? thanks – Jj Tuibeo Jan 10 '13 at 02:59
  • 2
    @JjTuibeo - that is what he is saying. The chance of this happening are (probably) small, but when it *does* happen you code is likely to break. – Stephen C Jan 10 '13 at 03:04
  • @JjTuibeo - Correct. It's a small chance but it is possible. It's avoided via the method I linked to. – Brian Roach Jan 10 '13 at 03:21

2 Answers2

2

You are using the same instance of the class StaticObjectReference to get to the queue, so yes they are the same queue.

Please note that this has nothing to do directly with the fact that instance is static though. It has more to do that this class is implementing the Singleton pattern, so there is only ONE instance of the class.

As Brian points out, this implementation isnt thread-safe. Check the wikipedia link for thread-safe methods.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
2

If an object reference is static, does it mean that the attributes of that object is static too?

This question does not make sense if you read it literally ... and that is probably the root of your uncertainty.

The term static (in Java) means that the variable belongs to a class, not an instance or a method call. The thing here that is labelled as static is a variable. The distinction between a variable, and the value contained in a variable is critical.

An object reference cannot be static. Static-ness is not a meaningful property for object references. It simply doesn't make sense. An object reference is a value, not a variable.

While a static variable holds a reference to an object, that (in general) doesn't make the object reference it contains (or the object it refers to) static. In addition to the terminological problem, if you change a static variable, the object that the variable originally referred to may go away. That is distinctly non-static behaviour.

In fact, the static-ness of the instance variable and queue are orthogonal issues. If the variables are declared as static, they are static. Otherwise they are not.

Now your code ensures1 that there will only ever be one instance of StaticObjectReference. But that is an emergent property of the way you have written the class, not anything to do with the declarations (static or otherwise) of those two variables. And we'd not call this property "static-ness". We'd call it "singleton-ness" ... if you'll excuse my abuse of the English language ...


1 - Actually, it doesn't always guarantee that because it is not thread-safe, as written.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216