Variables and methods that use static modifications are called class variables (or static variables) and class methods (or static methods), respectively. Variables and methods that do not use static modifications are called instance variables and instance methods, respectively.

Static keyword

Variables and methods that use static modifications are called class variables (or static variables) and class methods (or static methods), respectively. Variables and methods that do not use static modifications are called instance variables and instance methods, respectively.

Static variables

1. What are static variables

Use the static keyword to modify a member variable, which is called a static variable.

2. For example, if there are multiple circular objects, each circular object has its own radius and PI. Because PI is a fixed constant 3.141596, each circular object does not have its own. PI (PI) as shared, the radius of each circular object may be different.

3. How to use static variables

Such as:

Public class Ball {public double radius; Static double PI=3.141956; // PI public static void main(String[] args) {// TODO auto-generated method Stub Ball b1=new Ball(); // create the first round object Ball b2=new Ball(); // Create a second circular object, ball.pi =3.141596; // Static variable assignment b1.radius=5; // Assign the radius of the first circle to b2.radius=8; Println (" PI of the first circle object :"+ b1.pi +" "+" radius :"+b1.radius); System.out.println(" PI for the second circular object :"+ b2.pi +" "+" radius :"+b2.radius); // Prints the PI and radius of the second circle}}Copy the code

The output is:

The first circular object has a PI of 3.141956 and a radius of 5.0

The second circular object has a PI of 3.141956 and a radius of 8.0

In the above code, a static variable PI is defined in the Ball class to represent PI. It is shared by all instances because PI is a static variable and can be called either directly with ball.pi or with an instantiated object, b1.pi. The PI attribute for both the first and second circular objects is 3.141596.

Static method

1. What are static methods

Static variables and methods can be accessed without creating objects. Static methods are called when you add static to the prefix of a class definition method.

2. Static methods have the same access format as static variables

Class name. Method nameCopy the code

3. How to use static methods

public class Ball01 { public double radius; Public static double PI=3.141596; Public static double toRadius(double angdeg){return angdeg*(ball01.pi /180); } public static void main(String[] args) { // TODO Auto-generated method stub //Ball01 b1=new Ball01(); Print (ball01.toradius (100)); // Create a circle object system.out.print (ball01.toradius (100)); // Call the static method of the object}}Copy the code

The output is:

1.745331111111111 
Copy the code

In the above code, the Ball01 class defines a static method toRadius() Angle radius that calls the object’s static method through object instantiation. A static method can only access members that are statically decorated, because members that are not statically decorated need to create objects in order to access them, whereas static method calls do not need to create objects.

Static code blocks

1. What is a static code block

Static code blocks are lines of code surrounded by curly braces {} and decorated with the static keyword.

2. Execute as the class is loaded, and only once. Class member variables are typically initialized using static blocks of code.

3. Writing method of static code block and non-static code block

Static code block:

Static code block: static{} Non-static code block: {}Copy the code

4. How to use static code blocks

Such as:

class Person3{ static String name; Static {name=" li Hua "; System.out.println(" Static code execution for Person3 class..." ); }} public class test {// static code block static{system.out.println (" test class static code execution... ); } public static void main(String[] args) { // TODO Auto-generated method stub Person3 p1=new Person3(); Person3 p2=new Person3(); Create a second Person3 object}}Copy the code

The result of the run is:

  • Static code execution of the test class…
  • Static code execution for Person3 class…

As we can see from the results of the run, both static code blocks are executed. The first step is to load the test class, load the static code block along with the class, and then execute the main program (). By creating two Person3 objects in the main method and instantiating them twice, the static code is executed only once, indicating that it is executed as the class is loaded and only once.

Five, the summary

This article mainly introduces static keyword, static variable, static method, static code block.

Variables and methods that use the static modifier are called class variables and class methods, respectively. Variables and methods that do not use the static modifier are called instance variables and instance methods, respectively.

Static variables are member variables that use the static keyword. Understand how to use static variables. Static methods add static to the definition of a class. See how to use static methods in this example.

Static code blocks are lines of code surrounded by curly braces {} and decorated with the static keyword. Hope that through the study of this article, to help you!

Conclusion:

Thank you for reading this article, but also for you to prepare advanced Java architecture information, need friends can follow wechat public number ** [Java programmers gathering] ** to obtain architecture information.