Java member object inside a class of the same type

I look at the codebase and I often see something like:

public class SomeClass
{
 protected static SomeClass myObject;

 //...

 public static SomeClass getObject()
 {
  return myOjbect
 }
}

I would like to make sure that I understand the purpose of this. Does this mean that one instance of the class is shared, even if it is created multiple times? I am not sure about this dictionary, otherwise I would be looking for an answer, so if this template has a name, please let me know.

Also, this seems like a small definition of chicken and egg, because a class includes an object of type class. Why is this not paradoxical?

Thank!

+3
source share
5 answers

Singleton Pattern, . , , ( OO). Java AWT Swing, Frame/JFrame, main .

, , . ? ?

, ? , , static , , "" , - OO.

. , , .

+3

Singleton.

, , .

Wikipedia .

+4

"Singleton", , , . - , , , - , , , .

+3

, ; Singleton. - - - ; - , . , , , .

, , . - - " ".

+1

Singleton. (1) .

, , , .

public class SomeClass
{
 // Using private constructor
 protected static SomeClass myObject = new SomeClass();

 private SomeClass(){
 //...
 }

 public static SomeClass getObject()
 {
  return myOjbect
 }
}

, Wikipedia

Factory

+1

Source: https://habr.com/ru/post/1752644/


All Articles