1 Static code block
Here's a way to use static modifiers that are not very common. When we need a class, the constructor of this class is private, and other programs outside this class cannot create objects of this class. So how do you initialize this class?
To solve this problem, we can initialize the class using static member variables or static methods in the previous article. During the loading of a class, static member variables and static methods are loaded at the same time. In addition to the two methods above, there is a simpler way of using static blocks of code in a class. Static code block is not a method, it is simple to do, as follows:
public class Car {Privatecar () {} static{stringcolor= "Green "; SYSTEM.OUT.PRINTLN ("The color of the car is:" +color); System.out.println ("The car Stopped"); publicstatic void Main (string[] args) {}}
The results of the operation are as follows:
the color of the car is:Green
the car stopped.
This means that the program in the static code block is loaded simultaneously during class loading. So what can you write about in a static code block? Let's take the following code as an example to illustrate:
Public class car { privatecar () { } Stringcolor= "green"; staticstring mynewcolor= "BLACK" ; static{ stringmycolor= "White"; newcar (). Show ( ); stop (); systEM.OUT.PRINTLN ("The color of my car is:" +mycolor); system.out.println ("The new color of my car is:" +mynewcolor); } staticvoid stop () { system.out.println ("The car Stopped"); } voidshow () { SYSTEM.OUT.PRINTLN ("The color of the car is:" +this.color); }public Staticvoid main (String[] args) { }}
The results of the operation are as follows:
the color of the car is:Green
the car stopped.
the color of my car is:White
The new color of my car is:Black
The following conclusions can be drawn from the code:
1, only the static method can be called directly, but not directly call the non-static method;
2, if you need to call a non-static method also need to build the object to call a non-static method;
3. A variable declared in a code block is a local variable, so only generic variables can be declared and static member variables cannot be declared.
4. Static member variables can be accessed directly in the code block.
With the code above, we find that static blocks of code are powerful enough to initialize classes without building objects, addressing the requirements we mentioned above.
2 static inner class
If you define a class in a class again, the class that is redefined in the class is called an inner class. Although a class cannot use static modifiers, an inner class is a special case where static modifiers can be used.
2.1 defining static internal classes
Static cardoor
Public class car { publicstatic class CarWindow{ public void show () {           SYSTEM.OUT.PRINTLN ("The color of the car glass is black. ");} } Publicclass cardoor{ public void show () {  SYSTEM.OUT.PRINTLN ("The color of the door is white.") ");}} publicstatic void main (String[] args) { & nbSp; car.cardoorcardoor=new car (). New cardoor (); cardoor.show (); }}
To access the show () method in a non-static class , you need to define the object and then call the Show () method through the object . Because there are two classes of outer and inner classes, when you create an object, you create an external class object, and then you create an inner class object from an external class object. In the main () method, pass "Car.cardoor cardoor=new Car (). New Cardoor ();" statement creates a Cardoor object and then calls the Show () method through the Cardoor object .
The result of the operation is:
The color of the door is white.
to access a static inner class is slightly different from accessing a non-static inner class, mainly including accessing static members in static inner classes and statically members in static inner classes. The following will be described separately.
2.2 accessing non-static members in a static inner class
in the static inner class Carwindow defines a non-static method Show (), if you need access to this method, we first need to access the external class Car. However, unlike accessing a non-static inner class, when accessing a static inner class, because the static inner class is loaded with the external class, you do not need to create an external class object, but instead create the inner class object directly, using the inner class object to access the non-static method Show ().
to access The non-static method defined in Carwindow Show () requires that the main () methodbe modified as follows:
Public staticvoid Main (string[] args) {car.cardoorcardoor=new Car (). New Cardoor (); Cardoor.show (); Car.carwindowcarwindow=new Car.carwindow (); Window.show (); }
The result of the operation is:
The color of the door is white.
The color of the car glass is black.
2.3 accessing static members in a static inner class
When a static member exists in a static inner class, such as a static member variable or a static method, it is easier to access static members of the static inner class. Because static inner classes are loaded with the loading of external classes, static members in inner classes are loaded with the inner class loaded.
This means that when the external class is loaded, the static inner class and the static member are loaded at the same time. Therefore, it is possible to access static members in static internal classes without the need for objects, and access can be done directly using the class name.
the static inner class in the code above Carwindow 's Show () method is changed to a static method, the following modifications are made in the main () method:
Public staticvoid Main (string[] args) {Car.CarWindow.showCarWindow (); }
The result of the operation is:
The color of the car glass is black.
Although static methods for accessing static inner classes are simple to use, they are limited when defined as static. As mentioned in the previous article, static methods can only access static members, so it is not always appropriate to use static modifiers.
This article is from "Rookie Advanced" blog, please be sure to keep this source http://zyejl.blog.51cto.com/2769851/1792720
Introduction to how static keywords are used in Java four