Create a class - Java study notes

Construction method

  1. When a class is not provided any configuration method, the system provides a default constructor with no arguments, the constructor is called without parameter default constructor .
  2. When a class constructor is provided manually, then the system will not provide non-default constructor parameters; recommended to manually write constructor with no arguments, to prevent errors.
  3. The name of the constructor is the class name, and no return value
  4. Examples of variables initialized during construction will complete execution of the method, instead of loading the class time
    • If not manually assigned, then the system will automatically assign a value / Defaults
  5. Syntax - this (the actual parameter list):
    • The current through this constructor to call another class constructor,
    • Role: to achieve code reuse
    • Rote: For this () call can only appear in the first line of the constructor

Examples statement block

1. At what point execution?

  • As long as the construction method is performed, prior to constructing method performed necessarily automatically executing code "Examples statement block" in
  • This is the SUN company for programmers to prepare a special occasion, called the timing of the object constructor

2. What is the use?

  • A plurality of constructors have duplicate assignment section may assign portions of these common example sentence into blocks.
public class InstanceCode{
	
	//入口
	public static void main(String[] args){
		System.out.println("main begin");
		new InstanceCode;
		new InstanceCode("aaa");
	} 
	//实例语句块
	{
		System.out.println("实例语句块执行!");
	}
	
	//Constructor
	public InstanceCode(){
		System.out.println("无参数构造方法");
	}
	//Constructor
	public InstanceCode(String name){
		System.out.println("有参数构造方法");
	}
}

Package

Encapsulating code implemented steps of:

  • Property privatization
  • Foreign property provides a two set and get methods. External program can only be modified by the set, it can only be read by the get method, you can set up checkpoints in the set method to ensure the security of data
  • set and get methods when writing to write in strict accordance with regulatory requirements

Static variables and static methods

  • All the static keyword modifications are associated with the class, the class level
  • All static modification, are a "class name." The access is best not to use "references." Way, people will not distinguish whether it is static.
  • Static variables when the class loader initialization is complete, no new object corresponding to a class and method area stores in
  1. When declared as an instance variable, declared as static?
  • It is a property class level features rather than the object level feature
  1. Examples of methods and definitions when to when to define a static method?
  • This method generally describes a behavior, the behavior must go if triggered by the object. Then the method is defined as method of Example
  • Static methods can not call instance method, the instance attribute or write, because the object may not be instantiated .
  • However, the example method calls the static method, modify static variables would be no problem.

3. Reference Standard:

  • When this method body, direct access to the instance variables, then this method must be an instance method
  • In development, in most cases, if the words are tools, methods, tools which are generally static

Static code block

  1. Static keyword can be defined: static code block

  2. static static code block is executed at what time?

    • Execution-time class load and execute only once .
  3. Static block of code executed when the class is loaded and executed in the order from top to bottom before the main method is performed

  4. The role of static code block?

    • Static code block this mechanism is actually when grammar SUN's a special opportunity to programmers, this opportunity is called: class loading timing
    • Specifically, when the class is loaded, the class is loaded into the recording log information (e.g. time information) in the JVM; this logging code written in the static code block
public class StaticTest{
	//静态代码块
	static {
		System.out.println("A");
	}
	//一个类中可以编写多个静态代码块
	static {
		System.out.println("B");
	}
	//入口
	public static void main(String[] args){
		System.out.println("Hello World");
	}
	//在定义一个
	static int i = 100;
	static {
		System.out.println("i : " + i);
	}	
}

4. If the static variables defined after the static code block?

static {
    System.out.println("i : " + i);
}
static int i = 100;
  • Static variables and static code blocks are executed when the class is loaded, the same time can only be decided by the order code.
  • Compile Error: Illegal forward references

Null pointer exception

  • The following programs can be compiled, because grammatically
  • Null pointer exception occurred when running
  • Null reference access "instance" of the relevant data will appear famous exception --- null pointer exception: java.lang.NullPointerException
    • "Instance" of the relevant data representation: when the data access requires the participation of the object, this data is an example of relevant data
public static void main(Sting[] args){
	Students s = new Students();
	System.out.println(s.age);
	
	s = NULL;
	System.out.println(s.age);
}

Guess you like

Origin www.cnblogs.com/zy200128/p/12633705.html