(1) hands -on brain
The function does not have an initial value and then if the class provides a custom constructor method, it will cause the system not to provide a default constructor.
(2)
public class Test {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Initializeblockclass obj=new Initializeblockclass ();
System.out.println (Obj.field);//?
Obj=new Initializeblockclass (300);
System.out.println (Obj.field);
}
}
Class initializeblockclass{
{field=200;}
public int field=100;
public initializeblockclass (int value) {This.field=value;}
Public Initializeblockclass () {}
}
The result of the code execution:
The variables for all classes in the Java field are initialized to null by default, such as String A; Integer b;
The original data type of the numeric class is initialized to 0 by default, such as int A; Short B; char c;
The Boolean default is initialized to False. The default value that is specified when the class member definition is executed or the initialization module of the class exactly executes that one, to see which one is in the front row
2,: constructor for executing class
3: The initialization block of a class does not accept any arguments, and as long as an object of the transcend class, they are executed. Therefore, it is appropriate to encapsulate the code that must be executed when the object is created.
(3)
Execution Result:
The execution order of the static initialization module:
A: The static initialization block can only be executed once, and the creation of the subclass object also causes the static initialization block of the parent type to be executed once.
(3) An instance member of a class is accessed in a static method
Example:
public
class
ButtonFactory {
private
static
ButtonFactory _instance =
null
;
public
static
ButtonFactory getInstance()
{
if
(
null
== _instance)
_instance =
new
ButtonFactory();
return
_instance;
}
}
(4) The Paradox of integer
(5)
Using the class's static classes and constructors, we can keep track of the number of classes that a class creates. Please write a class at any time you can query how many objects you have created
Instance:
public class Objictnumber {
int number;
Public Objictnumber ()
{
++number;
System.out.println ("Currently created" +number+ "objects");
}
public static void Main (String[]args)
{
@SuppressWarnings ("unused")
Objictnumber obj=new Objictnumber ();
}
}
Java's hands on the brain October 20