This article describes the use and attention points of the Java static keyword:
1. Using the static Declaration property-that is, static declares the global property
2. Using the static declaration method-that is, calling the static method directly through the class name
Note the point:
When you use the static method, you can access only the properties and methods of the static declaration, not the methods and properties that are declared by static.
We declare three objects with the following code and assign them accordingly:
Package Hello;class people2{string name;int age;public People2 (String name) {this.name = name;} public void Tell () {System.out.println ("name:" + name + "Name:" + Age);}} public class Demostatic {public static void main (string[] args) {People2 P1 = new People2 ("Zhang San");p 1.age = 24;p1.tell (); People2 P2 = new People2 ("John Doe");p 2.age = 24;p2.tell (); People2 p3 = new People2 ("Harry");p 3.age = 24;p3.tell ();}}
We found that three people "age" are 24 years old, three times a repeat definition, then there is no simple way to define the "age" of three people are 24? Use the Static keyword to
The code is as follows:
Package Hello;class people2{string name;static int age;public People2 (String name) {this.name = name;} public void Tell () {System.out.println ("name:" + name + "Name:" + Age);}} public class Demostatic {public static void main (string[] args) {people2.age = 24; People2 P1 = new People2 ("Zhang San");p 1.tell (); People2 P2 = new People2 ("John Doe");p 2.tell (); People2 p3 = new People2 ("Harry");p 3.tell ();}}
Program output:
Name: Zhang San Name: 24 Name: John Doe Name: 24 Name: Harry Name: 24
The global property--age is declared with the static keyword, and it is assigned at the beginning of the main function: People2.age = 24
It is important to note that when assigning a value to the static property, the class name (PEOPLE2) is assigned directly, otherwise a warning is reported.
Next, we can also encapsulate the age property by private, with the following code:
Package Hello;class people2{string name;private static int age;public People2 (String name) {this.name = name;} public void Tell () {System.out.println ("name:" + name + "Name:" + age);} public static int Getage () {return age;} public static void Setage (int.) {people2.age = age;}} public class Demostatic {public static void main (string[] args) {people2.setage (24); People2 P1 = new People2 ("Zhang San");p 1.tell (); People2 P2 = new People2 ("John Doe");p 2.tell (); People2 p3 = new People2 ("Harry");p 3.tell ();}}
You can also accomplish the same effect.
"
When you use the static method, you can access only the properties and methods of the static declaration, not the methods and properties that are declared by static.
"
And how do you understand that?
The code is as follows:
Package Hello;public class Staticdemo {static int-age = 24;public static void Main (string[] args) {System.out.println (age) ; Tell ();} public static void-tell () {System.out.println ("Hello");}}
Can be seen,
1. The main function in Staticdemo can only invoke a method defined as static (static void Tell)
2. The main function in Staticdemo can only invoke properties defined as static (static int age)
Java Static keyword