Java Field Initialization rule:
Java field Initialization law: Variables of all classes 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;
Boolean is initialized to False by default; Java is initialized in two places: initialization blocks and constructors, where initialization blocks are divided into static initialization blocks and instance initialization blocks. Static initialization blocks are initialization blocks in a class that are decorated with static, and instance initialization blocks are initialization statements that do not have any keyword adornments in the class. If there is no formal parameter when creating an object in the main function, if a common variable is defined in the class and assigned, then the value is assigned to the variable in the main function, the default constructor in the class is called, and the corresponding constructor in the class is called if the object is created in the main function as a physical parameter.
Inherited:
The so-called "instantiate the object of the subclass before you instantiate the contents of the parent class, initialize for the parent class," refers to when the child class is being instantiated, always chaining calls to the parent class constructor method to initialize the parent class space. In other words, the constructor of a subclass must first call the constructor of the parent class before it can do any other initialization work.
public class jicheng{
public static void Main (string[] args) {
Stu s = new Stu ();
S.print ();
}
}
Class father{
String name;
{
Name= "Janny";
}
}
Class Son extends father{
int grade=;
void print ()
{
System.out.println ("Name:" +name+ "+" \ngrade: "+grade);
}
}
To statically access instance members:
The specific practice is to instantiate the class, get an instance variable, and pass the class as a parameter to the static method
static method (MyClass a) {//a Representation example variable
A.somemethod ();//Get a inside method
}
public class Ex
{
int i = 123;
static int Temp;
public static void A ()
{
System.out.println (Temp);
}
public int B ()
{
int x = 100;
return x;
}
public static void Main (stringargs[])
{
Ex e = new EX ();
Temp = e.b ();
E.a ();
}
}
After-school assignments:
public class A {
public static int count=0;
Public A ()
{
count++;
}
public static void Main (string[] args) {
A a=new a ();
A b=new a ();
SYSTEM.OUT.PRINTLN ("You have created the number of objects:" +a.count);
}
}
Java Classes and objects