Home > Back-end >  Why Java class of the static variables can hold its own object?
Why Java class of the static variables can hold its own object?

Time:11-28

The following is the code for the singleton pattern of the hungry:
 
Public class Singleton {
Private static final Singleton instance=new Singleton ();

Private Singleton () {}

Public static Singleton getInstance () {
return instance;
}
}

The problem is that the static initialize the Singleton instance variables, need new a Singleton object, then the Singleton class has not finished loading and initialization, and new a Singleton object needs to be loaded again good Singleton class, so don't fall into dead circulation?

CodePudding user response:

New a Singleton just calls the constructor, static variables and global about more than just a prefix,

CodePudding user response:

Static variables are initialized when loading a class a
Private static final Singleton instance=new Singleton () this is called private Singleton () {}

Will not be in infinite loop,

CodePudding user response:

refer to the second floor soton_dolphin response:
static variables are initialized when loading a class a
Private static final Singleton instance=new Singleton () this is called private Singleton () {}

Will not be in infinite loop,

"Static variables are initialized when loading a class", the problem in this sentence:
Will load the Singleton class to initialize static variables - & gt; To initialize static variables need new a Singleton object - & gt; New a Singleton object need to load the Singleton class, infinite loop!

CodePudding user response:

Class will first load, load in the process of the new object of this class, the class is not loaded completely, but has been loaded, not repeated load, the virtual machine will determine whether the class loading, is not loaded completely, loaded won't load for the second time, you think of death cycle, the designers of the Java virtual machine must have considered,

CodePudding user response:

reference WildGhost reply: 3/f
Quote: refer to the second floor soton_dolphin response:

Static variables are initialized when loading a class a
Private static final Singleton instance=new Singleton () this is called private Singleton () {}

Will not be in infinite loop,

"Static variables are initialized when loading a class", the problem in this sentence:
Will load the Singleton class to initialize static variables - & gt; To initialize static variables need new a Singleton object - & gt; New a Singleton object need to load the Singleton class, infinite loop!

The third step you are wrong, a new Singleton object does not need to be loaded when the Singleton class, it calls the constructor

CodePudding user response:

Static variables are initialized only once,

CodePudding user response:

Successively in the order is: load method in the Singleton class instance and static variables, the new Singleton () through the constructor initializes, pointing to the instance reference type variables!

CodePudding user response:

reference WildGhost reply: 3/f
Quote: refer to the second floor soton_dolphin response:

Static variables are initialized when loading a class a
Private static final Singleton instance=new Singleton () this is called private Singleton () {}

Will not be in infinite loop,

"Static variables are initialized when loading a class", the problem in this sentence:
Will load the Singleton class to initialize static variables - & gt; To initialize static variables need new a Singleton object - & gt; New a Singleton object need to load the Singleton class, infinite loop!


The new keyword is used to create a class object, instead of loading a class to the virtual machine,

CodePudding user response:

refer to the second floor soton_dolphin response:
static variables are initialized when loading a class a
Private static final Singleton instance=new Singleton () this is called private Singleton () {}

Will not be in infinite loop,

CodePudding user response:

references 9 f bcsflilong response:
Quote: refer to the second floor soton_dolphin response:

Static variables are initialized when loading a class a
Private static final Singleton instance=new Singleton () this is called private Singleton () {}

Will not be in infinite loop,

CodePudding user response:

reference WildGhost reply: 3/f
Quote: refer to the second floor soton_dolphin response:

Static variables are initialized when loading a class a
Private static final Singleton instance=new Singleton () this is called private Singleton () {}

Will not be in infinite loop,

"Static variables are initialized when loading a class", the problem in this sentence:
Will load the Singleton class to initialize static variables - & gt; To initialize static variables need new a Singleton object - & gt; New a Singleton object need to load the Singleton class, infinite loop!

A class is loaded once is enough, to load so many times?

CodePudding user response:

So what good?
  • Related