We can create multiple objects of that class based on a class, each with its own members, and sometimes we want all members of that class to share a member. This can be modified with static.
Static in Java, as a modifier, can be used to modify variables, methods, blocks of code.
1. Static method
A variable that is modified by static becomes a static variable and can be a static member or a class member. It belongs to the entire class, not all of an object, and is shared by all objects of the class. Class variables initialize the allocated space when the class is loaded, and are initialized only once until the class is unloaded before the resource is reclaimed.
Static members can be accessed directly using the class name or by using an object. Any object in the program modifies a static variable, and other objects see the modified value.
public class Test {static String play= ' Football ';p ublic static void Main (string[] args) {System.out.println ("Access static variable through class" +play); Test test=new test (); SYSTEM.OUT.PRINTLN ("Access static variables through objects," +test.play); test.play= "basketball"; SYSTEM.OUT.PRINTLN ("Access static variable via object," +test.play);}}
2. Static method
Static methods, called Static or class methods, are often used in the main method.
Static methods can call static variables directly in the same class, but cannot call non-static variables directly, you can access non-static variables by creating objects, static methods cannot directly call non-static methods, you can access non-static methods through the object.
public class Test {string Game= "DOTA"; static string play= "Football";p ublic static void Main (string[] args) {print ();} public static void print () {System.out.println ("Access static variable," +play);//system.out.println ("Access non-static variable," +game); The static method cannot directly access the non-static variable test test=new test (); SYSTEM.OUT.PRINTLN ("Access non-static variable," +test.game);//show ();//static method cannot directly invoke non-static method Test.show ();} In a common method, you can directly access a class of non-static variables and static variables public void Show () {System.out.println ("Access static variable," +play); SYSTEM.OUT.PRINTLN ("Access non-static variable," +game);p rint ();//normal method can call static method directly}}
3. Code block
Use static to modify a separate block of code inside a class called a static block of code.
Static initialization blocks are only executed at class load time, and only once, static blocks have no name, cannot display calls, and can only be called when the class is loaded.
Static initialization blocks can only assign values to static variables, and cannot initialize normal member variables.
public class test{string film; String game;static string play;public static void Main (string[] args) {test test=new test (); System.out.println ("film=" +test.film); System.out.println ("game=" +test.game); System.out.println ("play=" +play); Test test2=new Test ();} Public Test () {film= "Iqiyi art"; SYSTEM.OUT.PRINTLN ("Assign value by construction method film");} {game= "DOTA"; System.out.println ("Assign game by initialization block");} static {play= "football";//game= "DOTA";//cannot assign a value System.out.println ("Assign the play by static initialization Block");}}
Output Result:
Assigning a value to a play by static initialization block
Assigning a game by initializing a block
Assign a value by constructing a method film
film= Iqiyi Art
Game=dota
Play=football
Assigning a game by initializing a block
Assign a value by constructing a method film
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use of static in Java