Java Static keyword

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.