[Javase Study Notes]-7.8 characteristics of static keywords

Source: Internet
Author: User

[Javase Study Notes]-7.8 characteristics of static keywords

In this section, we will learn that a new keyword in java is the static keyword.

Of course, for each keyword, we need to clarify what role it plays and how it is used. Let's take an example:

Class Person {String name; // name String country = "China"; // nationality public void printInfo () {System. out. println (name + ":" + country) ;}} class StaticTest {public static void main (String [] args) {Person p = new Person (); p. name = "Xiaoqiang"; p. printInfo (); Person p1 = new Person (); p1.name = "Xiao Ming"; p1.printInfo (); Person p2 = new Person (); p2.name = "xiao hong "; p2.printInfo ();}}
Let's take a look at the running results of this example:

We will find that the names of the three objects are different, but the nationalities are the same. For many objects, the nationalities are the same, then we will think that we can't put the nationality independently into a space for all objects to directly reference, but in the object, we only need to have the name attribute.

The java language gives us a way to solve this problem, that is, the static keyword we are talking about now.

Obviously, we have clarified the role of the static Keyword: data that can be shared by all objects in the special data plane, also known as shared data of objects.

The above code is optimized with the static Keyword:

Class Person {String name; // name static String country = "China"; // nationality public void printInfo () {System. out. println (name + ":" + country );}}
Of course, the results are the results we see above, but in fact, the memory will store country in a separate space, and each object only has the name attribute, this saves a lot of unnecessary space.

We have just looked at the role of the static keyword, so next let's look at the characteristics of the static keyword?

Through the example above, we can summarize the following four features:

1. static is a keyword and modifier used to modify members.The Members here are member variables and member functions.

2. The data modified by the static keyword is shared data.We have seen that static is used to modify shared data, and the data stored in an object is the unique data of each object.

3. The members modified by the static keyword are loaded with the class.This is because the static modified data is shared by all objects, so it exists before the object is shared. Therefore, the java language provides the static function.

4. Members modified by the static keyword take precedence over objects,Because static members exist along with the faces of the class.

5. The static keyword modifier adds a call Method to the member, which can be called directly by the class name, that is, the call method of the Class Name and static member.

For example, in the above example, we can call:

Class StaticTest {public static void main (String [] args) {Person p = new Person (); // p. name = "Xiaoqiang"; // p. printInfo (); System. out. println (Person. country );}}
Result:

We can see that the modified members of staitc can be called directly using the class name, that is, we can call them directly without creating an object, because static members exist as the class is loaded, the shared data is shared by a small number of objects.

Of course, since our java is an object-oriented language, what a terrible thing if we modify all the members using static, that is to say, there is no object. However, in any case, sharing data is really cool.

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.