the rules for initializing Java fields:
Code
Package demo;
public class Initializeblockdemo
{
/**
* @param args
*/
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
{
The following sentence affects the initial value of the Field field before and after the initialization block
public int field=100;
{
field=200;
}
public int field=100;
public initializeblockclass (int value) {
This.field=value;
}
Public Initializeblockclass () {
}
}
Note that this is the Java field that is placed in the public int field=100;
Before the.
Run results
If the Java field is initialized and the public int field=100;
To change the position, the original code is as follows :
Package demo;
public class Initializeblockdemo
{
/**
* @param args
*/
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
{
The following sentence affects the initial value of the Field field before and after the initialization block
public int field=100;
public int field=100;
{
field=200;
}
public initializeblockclass (int value) {
This.field=value;
}
Public Initializeblockclass () {
}
}
Run results
So The law of the Java field initialization is in order, that is, the field initialization and the local variable initialization which depends on whichever.
1. When there is an inheritance relationship between multiple classes, creating a subclass object causes the execution of the parent class to initialize. Static methods can inherit, and can be overridden in subclasses! Static cannot be inherited.
2. Source code:
Package demo;
Class Root
{
static{
SYSTEM.OUT.PRINTLN ("Static initialization block of Root");
}
{
System.out.println ("Root's normal initialization block");
}
Public Root ()
{
SYSTEM.OUT.PRINTLN ("Root parameter-free constructor");
}
}
Class Mid extends Root
{
static{
SYSTEM.OUT.PRINTLN ("Static initialization block of mid");
}
{
SYSTEM.OUT.PRINTLN ("Normal initialization block for mid");
}
Public Mid ()
{
SYSTEM.OUT.PRINTLN ("Non-parametric constructor for mid");
}
Public Mid (String msg)
{
Calling overloaded constructors in the same class through this
This ();
System.out.println ("Mid with parametric constructor, its parameter value:" + msg);
}
}
Class Leaf extends Mid
{
static{
SYSTEM.OUT.PRINTLN ("Static initialization block of the Leaf");
}
{
SYSTEM.OUT.PRINTLN ("ordinary initialization block of the Leaf");
}
Public Leaf ()
{
A constructor that invokes a string argument in the parent class through Super
Super ("Java Initialization sequence demo");
System.out.println ("The constructor that executes the leaf");
}
}
public class Teststaticinitializeblock
{
public static void Main (string[] args)
{
New Leaf ();
}
}
Program run
The initialization of the parent class is invoked as soon as the child class object is created. The constructor of the parent class is also called as the child class object is created. The order of the calls is to call the static initialization block, call the initialization block, and finally call the constructor. When the three blocks are called, they are done in the order in which the parent class is called before the subclass is called again.
- Static methods access non-static variables:
Program Source code:
Package test;
public class Teststaticway
{
public static void Main (String args[])
{
Second s = new Second ();
S.show ();
}
}
Class Second
{
int A;
static void Show ()
{
Second m = new Second ();
System.out.println (M.A);
}
}
Program run
In the Java syntax, the static method is not directly accessible to non-static member variables, to access non-static member variables, can only be in the method body to create an object instance, that is, using the new keyword to create an object instance, Access to non-static member variables is accomplished through this object instance.
The type of wrapper class Byte,short,integer,long,character object that can use a constant pool when the value is less than 127 .
Source code and its comments:
Package test;
public class Strangecode
{
public static void Main (string[] args)
{
Integer i1=100;
Integer j1=100;
To take a number from a constant pool is to manipulate the same object in a constant pool. So the comparison result is true
System.out.println (I1==J1);
Integer i2=128;
Integer j2=128;
Two different objects are created in the heap space, their addresses are different, the output is false
System.out.println (I2==J2);
}
}
4. Create a class that can display the current number of objects in this class at any time
Source:
Package test;
public class Testcreatea
{
static int a = 0;
Public Testcreatea ()
{
a++;
System.out.println ("The number of objects currently in this class is" + A + ");
}
public static void Main (String args[])
{
Testcreatea t = new Testcreatea ();
}
}
Program Run Results
Programming Ideas:
Create a static constant A in the class body to record the number of objects, the initial value is 0, and then write a constructor, the statement of the constructor is the number of output objects and a++. Constructors are automatically called as objects are created, so each object is created to show the current number of objects.
The Third Classroom Experiment