Home > Back-end >  What is the difference between tectonic blocks of code and constructors?
What is the difference between tectonic blocks of code and constructors?

Time:09-30

Tectonic block: it is initialized to all objects, that is to say, all the object will be called a code block, as long as the object a set up, is called the code block,
Constructor: it is initialized to the matching object, it has targeted,

CodePudding user response:

Or premise is unknown, don't constructor also is a created object is called? Just call for different parameters of different constructor,

CodePudding user response:

The constructor is to create a new object initialization function of bai,

CodePudding user response:

The difference is:
Suppose you have multiple constructors, code in the constructor, only when invoking the constructor,
And tectonic blocks of code, no matter which constructor, you call all play a role,

A little bit more simple can be understood as a tectonic block is a common logic in the constructor of a code refactoring,

CodePudding user response:

Understood, for example:

Public class Test {

{
System. The out. Println (aaaaaa ");
}

The public Test () {
System. The out. Println (" BBB ");
}

The public Test (String a) {
System. The out. Println (" BBB "+ a);
}

{
System. The out. Println (" CCC ");
}

Public static void main (String [] args) {
New Test ();
New Test (" XXXXXXX ");
}

}
Run this code, the output is as follows:
Aaaaaa
CCC
BBB
Aaaaaa
CCC
BBBXXXXXXX

Construction method in the code, only in call this method when, while tectonic blocks of code when you call any constructor will run,

If you have multiple constructors have common logic, can reconstruct the tectonic block of code,

CodePudding user response:

Make up for the class initialization sequence, search relevant blogs
  • Related