1. a 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 initValue) {
value = initValue;
}
}
}
Conclusion: If the class provides a method of constructing custom, will not cause the system to provide default constructor. So call the class object is a mass participation should pay attention to the problem.
2. summed up the law Java field initialization
Code:
public class InitializeBlockDemo {
public static void main(String[] args) {
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(){
}
}
operation result:
Conclusion: performing the specified class member defining default or initialization block type, in the end we need to see which one of a "top surface", performed sequentially, the last execution 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."
3. hands-on brain: the static initialization block execution order
Code:
class Root
{
static{
System.out.println ( "Root static initialization block");
}
{
System.out.println ( "Root normal initialization block");
}
public Root()
{
System.out.println ( "Root constructor with no arguments");
}
}
class Mid extends Root
{
static{
System.out.println ( "Mid static initialization block");
}
{
System.out.println ( "Mid general initialization block");
}
public Mid()
{
System.out.println ( "Mid constructor with no arguments");
}
public Mid(String msg)
{
// Call overloaded constructor in the same class through this
this();
System.out.println ( "Mid constructor parameters with which the parameter values:" + msg);
}
}
class Leaf extends Mid
{
static{
System.out.println ( "Leaf static initialization block");
}
{
System.out.println ( "Leaf normal initialization block");
}
public Leaf()
{
// constructor has a super string parameter by calling the parent class
super ( "Java initialization sequence demo");
System.out.println ( "Leaf performed constructor");
}
}
public class TestStaticInitializeBlock
{
public static void main(String[] args)
{
new Leaf();
}
}
operation result:
Conclusion: 1 static initialization block is executed only once.
2. Create a sub-type of the object, it will lead to the implementation of static initialization block of the parent type.
4. 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)? Please write code to verify your ideas.
Code:
package Test;
public class Test
{
int x = 3;
static int y = 4;
public static void Tests() {
System.out.println ( "instance variables x =" + new Test () x.);
System.out.println ( "static variable y =" + y);
}
public static void main(String[] args)
{
Test.Tests();
Test t = new Test();
System.out.println("x = " + t.x);
}
}
Result of the program: