Hands-on brain


Why is the following code not compile? Where is wrong?


public class Test {
public static void main(String[] args)
{
Foo obj1=new Foo();
}
}
class Foo
{
int value;
public Foo(int intValue)
{
Value=intValue;
}
}

The program runs to be wrong, because if the class provides a custom constructor will cause the system to no longer provide a default constructor.

test------

public class InitializeBlockClass {
{
field=200;
}
public int field=100;
public InitializeBlockClass(int value)
{
this.field=value;
}
public InitializeBlockClass() {

}
public static void main(String[] args)
{
InitializeBlockClass obj=new InitializeBlockClass();
System.out.println(obj.field);
obj=new InitializeBlockClass(300);
System.out.println(obj.field);
}
}

Operating results for the 100 300.

Where there are two Java initialized: initialization block and a constructor, which is divided into initialization block static initialization block and instance initialization block.
Class static initialization block is modified by the static initialization block, instance initialization block for the class without any modification of the keywords initialization statement.
If you create an object in the main function in the absence of formal parameters, if you define a public variable in the class and give the assignment, it will assign values to variables in the main function,
and then call the default constructor of the class, If the physical reference object is created in the main function, the call corresponding to the class constructor.

Initialization law java field:

1. class constructor

(1) "constructor", also known as the "constructor", when an object is created, its constructor is called automatically. The method of the class name and the same configuration, there is no return value.

(2) If the class does not define a constructor, Java compiler will automatically compile time to provide it with a no arguments "default constructor." If the class provides a method of constructing custom, it will cause the system to no longer provide a default constructor

(3) the same class can have multiple constructors, to distinguish between a plurality constructor parameter. This is an example of method overloading.

We can call each other constructor.

2. The class initialization block

(1) you can use "{" and "}" will be surrounded statement directly as class members in the class.

This kind of "no name" and "members", used to initialize class fields.

3. When performing the specified class member define default or initialization block type, in the end we need to see which one of a "top surface."

Performing class constructor

Class initialization block does not receive any parameter as long as an object and a class is created, they will be executed. Therefore, the package is suitable for those "code must be executed when the object is created."


------- Run TestStaticInitializeBlock.java example, look at the output, concludes that "static initialization block execution order." --------

Run Results:
Root static initialization block
static initialization block Mid the
static initialization block Leaf of
ordinary initialization block of Root
Root no argument constructor
normal initialization block Mid the
Mid no argument constructor
Mid with constructor arguments which parameters: Java initialization sequence demonstrates
normal initialization block Leaf of
performing constructor Leaf


Summary:
1, the highest priority static initialization block, that is performed first, and only when the first execution in the class is loaded;
2, after the implementation of the non-static initialization block and a constructor, and execute whenever an object is generated once;
3, non-static initialization code block will be executed before the class constructor. To use therefore, should be to develop the initialization block written before the constructor of habit, easy to debug;
4, both static initialization block can be used to initialize static member variables, you can also execute initialization code;
5, non-static initialization block may be directed to a plurality overloaded constructor code reuse.

---------------------------
an interesting question:

Static method only allows access to static data, then, how in a static method access instance members of the class (ie no additional field or method static keyword)?

package Test;

public class test0

{

private static test0 _instance = null;

public static test0 getInstance()

{

if(null == _instance)

_instance = new test0();

return _instance;

}

}

-------------------------------------------------- ---------------
Integer of "strange" feature "

public class StrangeIntegerBehavior

{

public static void main(String[] args)

{

Integer i1=100;

Integer j1=100;

System.out.println(i1==j1);

Integer i2=129;

Integer j2=129;

System.out.println(i2==j2);

}

}


Operating results:
to true
false


To analyze the generated class file, see what it calls a method Interger class, and then open the JDK source file to view the source code, you can find the answer using javap.

 

Guess you like

Origin www.cnblogs.com/shenaoyu/p/11700105.html