Construction method: The construction method is automatically executed at compile time, each generation of a new object, the construction method will be executed in the object once, that is, only a new instance is produced to execute the construction method. Constructors can be used to initialize a specific value. The name of the constructed method is identical to the name of the class to which it belongs, no return type, and void, because void is actually returning null, except that NULL is omitted from the code. The construction method can be modified by public, private, etc. (see the single-state design mode when being modified by private). If a constructor is not defined in the class, then the default constructor is:
public class name ()
{
}
Of course, you can define multiple constructors at the same time, the system will no longer default to define the preceding constructor, and the same name as the member method of the same distinction, by passing the parameter type, number, etc. at the time of invocation automatically distinguish, as follows:
Class person
{
Int age;
String name;
Public person (INT.)
{
This.age=age;
}
public person (int age,string name)
{
This.age=age;//this (age);
This.name=name;
}
Public person ()
//{}
public static void Main (string[] args)
{
Person S1=new person ("29");
Person s2=new person ("Luo");
Person S3=new person ();
}
}
There is an error in the code above, the compilation will not pass, the corresponding constructor cannot be found in the class when the S3 instance is created, and the system does not produce a default constructor, so the constructor (the comment section) should be added.
This keyword: the This keyword is primarily used to emphasize an object that is currently calling a property or method. This is typically required in three cases. The first: As the above code, in the construction method to distinguish between the pass-through shape of the member variables (the same name, code to note legibility, at a glance) use the This keyword is used to describe the passed parameters assigned to the current instance of age, name. The second: The construction method is not directly called, because it is automatically called by the compiler, the constructor method calls the construction method, you can use the This keyword, such as the above Code (note section). Third: When adding a part to a container, indicate the added container, such as the following code:
Class container
{
Component comp;
public void AddComponent (component comp)
{
Comp=new component (this);
}
}
Class component
{
Public component (Container c)
{
This.c=c;
}
}
Main function: public static void Main (string[] args)
The main function marks the class in which it is located as a program startup class, where the program executes and allocates memory first, so non-static variables and non-static methods cannot be called. Because the main function is executed by a virtual machine call, it is public and cannot be private. String[] args are used to receive incoming arguments at run time (such as when compiling in a virtual machine, entering parameters after the compiled class name and passing in a space).
Static variables and methods are not individually owned by a class, but are common, stored in the method area, and are automatically initialized by the compiler. Non-static and non-static methods cannot be called in a static method, because non-static variables and methods are instances of the class, and it is not yet determined whether an instance has been produced, only new objects can invoke instance variables and methods. Static variables and methods can be called directly from the class name or accessed through the instance. From the storage point of view, the instance method and the variable are not allocated memory, so it cannot be accessed.
Garbage collection mechanism: classes and arrays stored in the heap become garbage when they are not referenced, and are not cleared immediately by default until the recovery mechanism starts scavenging at some point (not controlled by humans), or you can start collecting garbage using the System.GC () function. The public void Finalize () function is automatically called by the system at the first moment of cleanup. You can run the following code:
Class person
{
public void Finalize ()
{
System.out.println ("Rubbish is cleared");
}
Void obj ()
{
System.out.println ("Everything is ready");
}
public static void Main (string[] args)
{
New person ();
New person ();
New person ();
System.GC ();
}
}
Static code blocks: Static code blocks execute when a class is loaded, and execute only once (although instance objects with multiple classes) are often used to initialize class properties, in the following form:
Static
{
}
Single-State design pattern: Only one instance object can be created inside a class, and the constructor method is modified with private
Class China
{
Static China Obj=new ();//Here static cannot be saved, otherwise the instance object will be generated continuously
public static China getinstance ()
{
Return obj;
}
Private China ()
{
}
}
Class Test
{
public static void Main (string[] args)
{
China a1=china.getinstance ();//a1 and A2 are actually the same
China A2=china.getinstance ();
}
}
Java Learning Notes (i)