First, static basic description
For the keyword static, you should not be unfamiliar because you have been writing:
public static void Main (String args[]) {...}
What does that static mean? static denotes "global" or "static" meaning, used to modify member variables and member methods, or to form static code blocks, but note that there is no concept of global variables in the Java language.
defining attributes using static
Let's take a look at an operation class that represents a book, where all the books are published by Tsinghua Press.
Package Com.wz.thisdemo;
Class Book {
private String title;
private double price;
String pub = "Tsinghua University Press"; Public book
(String title, double price) {
super () is temporarily not encapsulated here;
this.title = title;
This.price = Price;
}
Public String GetInfo () {return
"title:" + This.title + ", Price:" + This.price + ", publishing house:" + this.pub;
}
}
public class Testdemo {public
static void Main (String args[]) {book
ba = new book ("Java Development", 10.2);
Book BB = new book ("Android Development", 11.2);
Book BC = new Book ("Oracle Development", 12.2);
System.out.println (Ba.getinfo ());
System.out.println (Bb.getinfo ());
System.out.println (Bc.getinfo ());
}
Run Result:
Title: Java Development, Price: 10.2, publishing House: Tsinghua University Press
title: Android Development, Price: 11.2, Press: Tsinghua University Press
title: Oracle Development, Price: 12.2, Press: Tsinghua University Press
The memory allocation diagram is as follows:
So the question is, since all of the books in this class have the same publishing house, is it necessary for each diagram object to have duplicate attribute information?
Suppose there are 10 million book class objects that require the replacement of the publisher name of all objects, which means something.
Let's look at the code and add the following in the main method:
public class Testdemo {public
static void Main (String args[]) {book
ba = new book ("Java Development", 10.2);
Book BB = new book ("Android Development", 11.2);
Book BC = new Book ("Oracle Development", 12.2);
Ba.pub = "Peking University Press";
bb.pub = "Zhejiang University Press";
System.out.println (Ba.getinfo ());
System.out.println (Bb.getinfo ());
System.out.println (Bc.getinfo ());
}
Run Result:
Title: Java Development, Price: 10.2, Press: Peking University Press
title: Android Development, Price: 11.2, publishing house: Zhejiang University Press
title: Oracle Development, Price: 12.2, publishing house: Tsinghua University Press
This means that you want to modify the contents of 10 million objects. So, if you define an object's properties as normal, this means that each heap of memory will hold its own information.
since the pub content of each book object is the same, it should be defined as a shared property, that is, all objects share the pub attribute. So, in this case, to describe the concept of sharing, you can use static to define this property that needs to be shared.
Use the static modifier on the property pub of the book Class:
The Main method is:
public class Testdemo {public
static void Main (String args[]) {book
ba = new book ("Java Development", 10.2);
Book BB = new book ("Android Development", 11.2);
Book BC = new Book ("Oracle Development", 12.2);
Ba.pub = "Peking University Press";
System.out.println (Ba.getinfo ());
System.out.println (Bb.getinfo ());
System.out.println (Bc.getinfo ());
}
Run Result:
Title: Java Development, Price: 10.2, Press: Peking University Press
title: Android Development, Price: 11.2, Press: Peking University Press
title: Oracle Development, Price: 12.2, Press: Peking University Press
We find that once a static is used on a property definition, as long as an object modifies the contents of the property, the contents of the static property of all objects are modified together. At this point, the pub property becomes a public property.
As a result, the memory allocation diagram becomes the following:
The biggest difference between the properties of a static declaration and the normal property (not static) is the difference between saving the memory region.
Another problem: Since a static-defined attribute is a public property, it is not appropriate to simply modify the property by an object. The appropriate approach is to modify the public representation of all objects-classes to complete them through the class name. static property.
So the above:
Ba.pub = "Peking University Press";
Most useful
Book.pub = "Peking University Press";
To replace.
Through the above explanation, we should know the following points:
(1) Properties defined using static are not saved in heap memory and stored in the global data area;
(2) using static-defined attributes to represent class attributes, which can be invoked directly by the class name;
(3) The static property, although defined in the class, can be invoked without instantiating the object (the normal property is stored in the heap memory and the static property is stored in the global data area);
When you define a property, when you use the static definition. When do you not use the static definition?
In development, as far as possible, do not use static to define attributes, if you need to describe the sharing of information, use the static definition () is mainly for the convenience of collective modification, and do not allocate memory space.
defining methods using static
In addition to defining attributes with static, the method can also be defined using static, so it is clear that a method defined with static can also be invoked directly by the class name without instantiating the object.
Look at a piece of code:
Package Com.wz.thisdemo;
Class Book {
private String title;
private double price;
private static String pub = "Tsinghua University Press";
Public book (String title, double price) {
this.title = title;
This.price = Price;
}
static method public
static void Setpub (String p) {
pub = P;
}
Public String GetInfo () {return
"title:" + This.title + ", Price:" + This.price + ", publishing house:" + this.pub;
}
}
public class Testdemo {public
static void Main (String args[]) {
//Call before object generation
book.setpub ("Peking University Press"); C20/>book ba = new book ("Java Development", 10.2);
Book BB = new book ("Android Development", 11.2);
Book BC = new Book ("Oracle Development", 12.2);
System.out.println (Ba.getinfo ());
System.out.println (Bb.getinfo ());
System.out.println (Bc.getinfo ());
}
Run Result:
Title: Java Development, Price: 10.2, Press: Peking University Press
title: Android Development, Price: 11.2, Press: Peking University Press
title: Oracle Development, Price: 12.2, Press: Peking University Press
But the problem is that there are two methods in the class, one is the static method, the other is a non-static method, and the access between the two methods is limited.
(1) A static method cannot directly invoke a method or property that is not static;
(2) A non-static method can call a static property or method, unrestricted.
But why is there such a limit?
properties and methods defined with static can be used when no object is instantiated, rather than static-defined properties and methods, which must be instantiated before the object can be invoked.
So, we use the static method. When do I use the static method?
if there are no attributes in a class, only methods, it is recommended that all methods be defined as static methods , so that the object is not instantiated each time the method is invoked. In practical development, it is recommended that the non static method and the non static property be given priority.
four Java main methods
Java method Definition, if a method is defined in the main class and is called directly by the main method, then the preceding must have a public static, in the following format:
public static Returns a value type method name (argument list) {
[return value];]
}
Observe the following code:
Package Com.wz.thisdemo;
public class Testdemo {public
static void Main (String args[]) {
print (); Direct call
} public
static void print () {
System.out.println ("Hello world.");
}
Run Result:
Hello World.
At this point, a static method is invoked to invoke the other static method, but if there is no static on the print () method.
Package Com.wz.thisdemo;
public class Testdemo {public
static void Main (String args[]) {
print (); Direct call
} public
void print () {
System.out.println ("Hello world.");
}
Run Result:
Exception in thread ' main ' java.lang.Error:Unresolved compilation problem:
cannot make a static reference to the non -static method Print () to the type Testdemo at
com.wz.thisdemo.TestDemo.main (testdemo.java:5)
almost all of the non-static methods have a feature: The method is invoked by the instantiated object. the correct wording is as follows:
Package Com.wz.thisdemo;
public class Testdemo {public
static void Main (String args[]) {
new Testdemo (). print (); Object Call
} public
void print () { //non-static method
System.out.println ("Hello world.");
}
Run Result:
Hello World.
Now, to see the composition of the Main method:
public static void Main (String args[]) {...}
You can see that there are a lot of keywords inside:
(1) Public: It is a kind of access, which means a common;
(2) Static: This method is directly called by the class name;
(3) Void: The Main method is the beginning of the execution of the program;
(4) Main: The system specified a method name, the implementation of the class when the default find this name, can not be modified;
(5) String args[]: Represents a parameter passed by a program at run time, received by a string.
v. Static use examples
Count the number of instantiated objects produced by a class
Analysis: A class can certainly have multiple instantiated objects to produce, add a statistical operation, can be counted out of a class of the resulting number of instantiated objects. If you now want to generate a new instantiated object, you will certainly invoke the constructor method in the class, so you can increase the statistics in the constructor method, and the statistic variable is shared by all objects and should be defined as a static property.
Package Com.wz.thisdemo;
Class obj{
private static int count = 0;
Public Obj () {
System.out.println ("Count =" + ++count);
}
}
public class Testdemo {public
static void Main (String args[]) {
new Obj ();
New OBJ ();
New OBJ ();
New OBJ ();
New OBJ ();
}
Run Result:
Count = 1
count = 2
count = 3
count = 4
count = 5