Key points
The Java programming idea describes the static method: "The static method is the method without this." A non-static method cannot be called inside a static method, which in turn is possible. The Staic method can be called only through the class itself, without creating an object . This is actually the primary use of the static method. ”
Although it only describes the static method, you can see the main role of the static keyword: without creating an object, call a static method through the class itself, access the static domain, get a static constant, execute a static block while the class is loading, and import the static and static constants in the package.
1. Static domain (rarely used)
If you define a domain as static, there is only one such domain in each class. Each object has a copy of its own for all instance domains.
class student{privatestatic String faculty = "Computer Academy"; ...}
As above, if this class creates more than one sutdent instance, all instances will share the faculty, even if there is no object, the static domain exists, and it belongs to the class and does not belong to any independent object.
2. Static constants (Common)
Unlike non-static constants, an instance of each class has a common constant, and only one static constant exists throughout the program.
Public Final int Year =; Public Static Final int Year = 2015; More space-saving
3. Static methods
A static method is a method that cannot be manipulated against an object, which does not have this parameter and cannot access the instance domain. Called by Class.method. (It can also be called using object.method like the Invoke instance method, but not recommended)
class student{ ... Public Static String getfaculty () { return faculty; } ...} // student.getfaculty () call with Class.method
Use static methods in the following situations:
1) a method does not require access to the object state, all of its parameters are provided by displaying parameters
2) A method only needs to access the static domain of the class
Limit
1) not upgradeable, rewrite
2) Fixed memory consumption, not released
4. Static block
Feature: When a class is loaded, the object is created before it is executed, and is executed only once throughout the application's life cycle.
public class Test { public static void main (string[] args) {mytest a = new mytest (); MyTest b = new mytest (); }} class mytest{ static {System.out.println (" Static block Execution "); public MyTest () {System.out.println ( span> "constructor Execution" Execution result:
Static block execution
Constructor execution
Constructor execution
5. Static Import
Import static and static constants by using Import-static XXX
Import Static java.lang.system.out;out.println ("Hello world!") ;//Can be used out without System.out
code example:
1 ImportJava.util.Random;2 3 Import StaticJava.lang.System.out;//Static Import4 5 classstuenttest6 {7 Public Static voidMain (string[] args) {8student[] Test =NewStudent[3];9Test[0] =NewStudent ("Zhang San", "20151000153", "01", 3.4);TenTEST[1] =NewStudent ("John Doe", "20150000231", "02", 2.9); OneTEST[2] =NewStudent ("Harry", "20151000462", "03", 4.2); A - DoubleAverage = 0; - for(Student i:test) { theOut.println (I.getname () + "+student.year+" +student.getfaculty () + "+student.getfacilty_id () +" + i.getStu_id () + " + i.getclass_id () + "+" +I.getgpa ()); - //class name. Method name Call static method, get static constant -Average + =I.getgpa (); - } +Out.println ("Average_gpa:" + AVERAGE/3); -Student.setfaculty ("School of Arts and Media");//changing Static Constants + for(Student i:test) { AOut.println (I.getname () + "+student.getfaculty ()); at } - } - } - - classStudent - { in PrivateString name; - Public Static Final intYear = 2015;//Static Constants to Private StaticString faculty = "Computer Academy";//Static Variables + Private Static intfacilty_id; - PrivateString stu_id; the PrivateString class_id; * Private DoubleGPA; $ Panax Notoginseng //static Block - Static { theRandom generator =NewRandom (); +facilty_id = Generator.nextint (19); A } the + PublicStudent (String name,string stu_id,string class_id,DoubleGPA) { - This. Name =name; $ This. stu_id =stu_id; $ This. class_id =class_id; - This. GPA =GPA; - } the - Public StaticString getfaculty () {Wuyi returnfaculty; the } - Wu Public Static intgetfacilty_id () - { About returnfacilty_id; $ } - - PublicString GetName () { - returnname; A } + the PublicString getstu_id () { - returnstu_id; $ } the the PublicString getclass_id () { the returnclass_id; the } - in Public DoubleGetgpa () { the returnGPA; the } About the Public Static voidsetfaculty (String str) { theFaculty =str; the } +}
Execution Result:
Zhang 32,015 Computer College 7 20151000153 01 3.4
Li 42,015 Computer College 7 20150000231 02 2.9
Wang 52,015 Computer College 7 20151000462 03 4.2
average_gpa:3.5
Zhang San College of Arts and media
John Doe College of Arts and media
Harry College of Arts and media
Simple analysis of static keywords in Java