Hello!

This code is often found in the code (in any C-like language).

class One { One instanse; //Вот эта строчка не понятна ... //Здесь дальнейшая реализация } 

Such a line is always found in a singleton and (if I understood correctly) is called a global access point. The field of the class itself, in which the object of the class is enclosed, leads me to the idea of ​​recursion, which is completely incomprehensible.

Questions:

  1. What does this field mean?
  2. What is this field used for?
  3. How is that even possible? After all, a class is the essence of a stamp, and an object is a cast from a stamp. It turns out the stamp contains a casting? Does each copy already contain a copy of itself?
  4. Optionally - where and in what book (website) is it clearly described?
  • 2
    static One instanse; - Igor
  • sometimes private static One instance . - Andrew Kachalin
  • @Igor I have compiled the following code in java: public class SingleT { public int x, y; SingleT instance; SingleT(int x , int y){ this.x = x; this.y = y; } public static void main(String[] args) { SingleT st = new SingleT(5, 4); } } public class SingleT { public int x, y; SingleT instance; SingleT(int x , int y){ this.x = x; this.y = y; } public static void main(String[] args) { SingleT st = new SingleT(5, 4); } } public class SingleT { public int x, y; SingleT instance; SingleT(int x , int y){ this.x = x; this.y = y; } public static void main(String[] args) { SingleT st = new SingleT(5, 4); } } - Andrew Kachalin
  • @AndrewKachalin That construction, like yours (without static ), may be suitable, for example, to build a прадед-дед-отец-сын-внук... chain прадед-дед-отец-сын-внук... Those. each object contains a descendant of its own class. If with static - then it is 99% singleton. You can read about it yourself - rjhdby
  • one
    @rjhdby valid, but not in the context of a singleton) - Alexey Shimansky

1 answer 1

Here in this form

 class One { One instanse; } 

This is a common property of the object. No one prevents an object from having a property of the same class to which it itself belongs. For example, in this way you can implement the poem "A tree behind a tree, and a tree behind a tree, and a tree behind a tree, so the forest ended (that is, the last object is null )". In principle, any tree structure (recursive) structure can be described this way. Here is a binary tree node for example:

 class Node { Node left; Node right; } 

If we see this

 class One { private static One instanse; } 

Here this field is a class field. Those. it is common to all class instances. Usually such constructions, along with the private constructor and the getInstance() method:

 private One(){...} public static One getInstance() { //самая простая реализация if(instance == null) instance = new One(); return instance; } 

Ensures that one and only one instance of this class will be created, which will be stored in the instance field and retrieved via One.getInstance() . Those. singleton

  • Did I understand you correctly that the property of the object and the class field are not the same? I simply believed that the object characterizes: behavior (ie, methods), entity (ie, fields), state (ie, the value of fields). - Andrew Kachalin
  • @AndrewKachalin is certainly not the same thing. Read what the static - rjhdby modifier means
  • In order to be NULL in the last object, it must be a pointer. - user239133
  • @AlexanderZonov tags ооп , любой язык . Java for example. - rjhdby
  • @AlexanderZonov well, in terms of java, this field (property) is rather not an object, but only a reference to an object, that is, you can say, a pointer. - Andrew Kachalin