0

Hi All this can be duplicate question sorry for that but i was not able to find that post.

Question :- Suppose there is a class A which has been written in this way

  @Component
  public class A{}

Now when i call A a = new A() two times will it provide me same object or not ? This may be stupid question but Can you please clarify it in details ?

Thanks,

Paras Meena
  • 191
  • 4
  • 11
  • 2
    You don't have a Bean when using the `new`keyword yourself, you have a POJO. A Bean implies that the object is created by the container. This said, the bean container will always return the same bean. Using `new`, you would have to implement the singleton pattern on the POJO or you will end up with different objects. – Nico Van Belle Jun 01 '18 at 10:57
  • 2
    in a singleton, you shouldn't be able to call the constructor from outside the class itself. – Stultuske Jun 01 '18 at 10:57

2 Answers2

1

When you call A = new A() in your example, you'll get a new instance, always, because A is not implemented as a singleton class.

The fact that it is anotated as @Component will only affect that class when it is instanced in the spring context, and a variable instantiated with = new() (there are exceptions, but lets generalize) is not in the spring context.

If you want to always have the same bean, you should instantiate your variable "a" with @Autowired, in the following way:

@Autowired
private A a;

Also note that @Autowired will only work if the current class is, too, in the spring context (you didn't instantiate it with a =new(...)).

Jorge.V
  • 1,329
  • 1
  • 13
  • 19
  • Thanks for explanation, I don't want to instantiate same object or use injection, I was just want to clear this doubt that will spring singleton will work as java singleton class or not. :) – Paras Meena Jun 01 '18 at 12:47
1

First of all, this one is Singleton class example and you cannot instantiate it with new keyword from outside of class because your constructor is private.

 class MySingleton
{
    static MySingleton instance = null;
    public int x = 10;

    // private constructor can't be accessed outside the class
    private MySingleton() {  }

    // Factory method to provide the users with instances
    static public MySingleton getInstance()
    {
        if (instance == null)        
             instance = new MySingleton();

        return instance;
    } 
}

Secondly, you can find lots of information in here about Bean and you need to create bean with @Bean annotation for example.

Besides, you can look this post

Hatice
  • 874
  • 1
  • 8
  • 17
  • "you cannot instantiate it with new keyword." this is a bit incomplete, seeing as you instantiate it with the new keyword as well. a more complete explanation: "you cannot instantiate it with new keyword from outside of it's own class" – Stultuske Jun 01 '18 at 11:19
  • This is not a singleton class this is a singleton bean, there are many difference in java class and spring bean. – Paras Meena Jun 01 '18 at 12:49