0

"In Java the Singleton pattern will ensure that there is only one instance of a class is created in the Java".

I am not able to understand single instance means here.

For example :

 A  a = new A();

here what is a ? is it object or instance? if a is instance, does it mean that we can't do like below.

A b = new A() i.e. another instance 'b'

any diagrammatic example would help me.

Thanks

user3448119
  • 107
  • 10
  • Possible duplicate of [What exactly is an instance in java?](http://stackoverflow.com/questions/5126082/what-exactly-is-an-instance-in-java) – ylun.ca Mar 21 '14 at 20:51
  • Also, objects and instances are the same thing.. – ylun.ca Mar 21 '14 at 20:52
  • A good singleton wouldn't even expose its constructor like this, so this wouldn't even compile. It would probably be something like A a = A.getInstance(); A b = A.getInstance(); where they have the same instance – DangerDan Mar 21 '14 at 20:54
  • I don't think `A` is supposed to be the singleton class. I think `A` is just being used to explore the question "What is an instance?" so the OP can understand the statement about singletons. – David Conrad Mar 21 '14 at 21:10
  • Yes, it's what it means. Speaking the simplest words, it means you can't call new A() twice or more times. And yes instance = object. – luke1985 Mar 21 '14 at 21:13
  • An Object is an instance of a Class. It is dangerous to say that Objects and instances are the same thing, because an instance is just an instantiation of some construct, and using instance and Object interchangeable might cause some confusion down the road. For Java, I believe they are the same, but that is only because of the lack of constructs other than Classes.... EDIT: Another Java construct could be an Interface, which although you can't instantiate it directly, you can still have an instance of one. So no, Object != instance. – MirroredFate Mar 21 '14 at 21:39

4 Answers4

0

In your example, a is a reference to an instance of A, that got created by calling new A().

You can very well do A b = new A();, this means that now you have two instances of A around.

And an object means the same as an instance.

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • thanks. But I read in SO, Class is blue print, object is like house built from it. particular house is called a instance!? i mean there is difference between object and instance – user3448119 Mar 21 '14 at 20:56
  • 1
    Hey, that was my answer. :) I just updated it. Object and instance are the same thing. – yshavit Mar 21 '14 at 20:57
0

a there is a reference to the newly created object that new A() creates. That has nothing to do with the singleton pattern, though; that's always true.

In the singleton pattern, you would find a way of ensuring that new A() is only called once ever in the program. The typical way this is done is to make A's constructor private, and create the instance from within A (which is thus the only class allowed to call the private constructor).

public class A {
    public static final A instance = new A();

    private A() {}

}

This approach does have a few edge cases, though: you can create extra instances through reflection, and through serialization if A is serializable. To really make a singleton, the best way in Java is to use enums:

public enum A {
    INSTANCE;
    // fields, methods etc go here
}

The JVM will ensure that only one A.INSTANCE exists in the process.

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • thanks. it answers my questions. but here the magic of having only instance although making private constructor is that static final right? without static, would it be possible to make it singleton class? – user3448119 Mar 21 '14 at 20:58
  • implementing singleton using enum is nowadays is famous is that true? – user3448119 Mar 21 '14 at 20:59
  • Without `static`, you'd only instantiate that `instance` when you instantiate an `A`. This has two problems: First, you can never do it (how do you instantiate `instance` without first having an instance?) and second, it would cause an infinite loop that ends in a `StackOverflowException` (as each instance would instantiate a new instance, which would instantiate yet another instance, etc). I'm not sure what you mean by the enum approach being "famous". – yshavit Mar 21 '14 at 21:01
0

Here 'a' is reference which is pointing to the 'new A()' created.Once an object is created you can't create other object while using singleton pattern.Using A b = new A() is wrong.

RKC
  • 1,834
  • 13
  • 13
0

objects ARE instances. instances (= a case or particular example) of classes, which are some things written in programming language that define the semantics of objects themselves. When you use a singleton you define a class with a private constructor, this means that only instances of that class can call the constructor, so only instances of that class can create other instances of that class. the next step is to write a method that says. "do you want a reference to an instance of this object? well, if anyone asked this to me before, i'll create it and then i'll give it to you, or rather i give to you an instance already created".

So now you are asking "wait, if i need an instance of this class to get an instance of this class, how can i get an instance of this class without an instance of this class??!?". therese a simple answer to a complicated question: use static methods and fields

static code does not belong to any instance of the class, static variables and static method code are stored in other zones of address space, so you can use it before you have an instance of the singleton class

Carmine Ingaldi
  • 856
  • 9
  • 23