02 day 9-0 basic self study java static method, instance method, static static code block, instance code block

Static methods: methods decorated with static Instance method: method without static decoration Call static method 1, ...
Call static method
Call instance method
When to define as instance method and when to define as static method
Static code block
A lot of code has been executed before the main method is executed
Can I access instance variables?
Static code block is above static code
Instance code block (understanding)
Code execution order

Static methods: methods decorated with static
Instance method: method without static decoration

Call static method

1, "Class name. Method name ();"
2, "Reference. Method name ()“

Call instance method

"quote.Method name();" public class StaticTest04{ public static void main(String[] args){ StaticTest04.doSome();//Calling static methods by class name StaticTest04 s1=new StaticTest04(); s1.doOther();//Call instance methods by reference s1.doSome();//Calling static methods by reference } public static void doSome(){ System.out.println("Static method executed"); } public void doOther(){ System.out.println("Instance method executed"); } }

When to define as instance method and when to define as static method

When the instance variable is directly accessed in the method body, the method can only be an instance method (object related)
(in our future development, in most cases, if it is a tool class, we use a static method.)

Static code block

1. Use the static keyword to define: static code blocks
2. What is static code block and what is syntax?

static{ java sentence; java sentence; }

3. When does static static code block execute?
Class is loaded and executed only once.
4. Static code blocks are executed when the class is loaded, so they are executed before the main method is executed
5. Static code blocks are usually executed in top-down order.
6. Function of static code block:
First, static code blocks are not so common
Second, SUN company gives java programmers a special period of class loading in the static code block syntax mechanism.
Specific business:
The project manager said: Please note that in all the programs we write, as long as the class is loaded, please record the log information of class loading (these logs are written into the static code block)

public class StaticTest{ static{ System.out.println("hello"); } /*public static void main(String[] args){ System.out.println("world"); } Will print hello, but will prompt error no main entry found*/ }

A lot of code has been executed before the main method is executed

public class StaticTest{ static{ System.out.println("hello"); } static int i=11;//Class initialization on load static{ System.out.println("i="+i);//Access to i } public static void main(String[] args){ System.out.println("world"); } }

Summary:
Stack: as long as the method is executed, it will stack (local variable)
Heap: the objects out of new are all in the heap, and the garbage collector mainly aims at (instance variable)
Method area: class information, bytecode information, code snippet (static variable)

Can I access instance variables?

public class StaticTest{ /* int k=111; static{ System.out.println("k="+k); } The instance variable cannot be accessed because it is opened and initialized in heap memory when the new object is used. It does not exist when the class is loaded*/ public static void main(String[] args){ } }

Static code block is above static code

public class StaticTest{ static{ System.out.println("k="+k); } static int k=100; public static void main(String[] args){ } } Error will be reported: illegal forward reference (both static code blocks and static variables are executed at the same time when the class is loaded, and only the order of the code can determine who is the first and who is the second)

Instance code block (understanding)

public class InstanceCode{ public static void main(String[] args){ System.out.println("main Method executed"); new InstanceCode(); } public InstanceCode(){ System.out.println("Parameterless construction method executed"); } { System.out.println("Instance statement executed"); } public InstanceCode(String s){ System.out.println("The parameter construction method is executed"); } }

The instance statement block is executed only when the constructor is called, and it is executed first, no matter where the instance statement block is. And the call is executed once. (it can be applied reasonably, such as taking a split line when calling the construction method)

Code execution order

9 June 2020, 23:54 | Views: 8430

Add new comment

For adding a comment, please log in
or create account

0 comments