Java Study Notes-12. Object-Oriented Programming 07-static keyword usage in Java

Source: Internet
Author: User

The static keyword is the main content of this afternoon. The static keyword is another difficulty beyond this, so it is also listed separately for taking notes.

There are three static keyword application points: Declaration and reference of static variables (attributes), declaration and reference of static methods, and staticCodeBlock usage. It seems that there are few opportunities to use static code blocks ~ The first two parts of the main note.

1. Declaration and reference of static attributes (variables)

Static attributes are attributes that are listed separately by the Public attributes (that is, the attributes of all objects are the same), and static attributes are used. here, we need to explain why static attributes are required.

Let's take a look at the following example code:

Class person {// For convenience, the property is not encapsulated with string name; int age; // The default city is Nanjing string city = "Nanjing"; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String getinfo () {return "name:" + name + ", age:" + age + ", city:" + city ;}} public class demo01 {public static void main (string ARGs []) {person p1 = new person ("Zhang San", 30); person P2 = new person ("Li Si ", 31); person P3 = new person ("Wang Wu", 32); system. out. println (p1.getinfo (); system. out. println (p2.getinfo (); system. out. println (p3.getinfo ());}}

The usage of the above example in memory is analyzed here, for example:

It can be seen that all three objects have an identical property city = "Nanjing", so there will be problems with this design:
(1). The property content already exists, and all objects have this property;
(2) If Nanjing has changed to Beijing, and 100000 objects have been generated, you must modify the attribute of the city to 100000 times.

Solution:
It is best to point all objects to the same city attribute. If one object modifies the city attribute, others will be affected. In this case, you need to use static to identify the city attribute. The instance diagram is as follows:

The following is an example of using the static keyword to declare a class property. In this example, you can access and modify the static property in the format of the class name. Static Property.

 class person {// For convenience, string name; int age; static string borncity = "" is not encapsulated for the moment "; public Person (string name, int age) {This. name = Name; this. age = age;} Public String getinfo () {return "name:" + name + ", age:" + age + ", nationality:" + borncity ;}} class oodemo06 {public static void main (string ARGs []) {person p1 = new person ("Shao Yifu", 60); person P2 = new person ("Bao yugang", 61 ); person P3 = new person ("Chiang Kai-shek", 63); system. out. println ("--------------------- -----------------------"); system. out. println ("\ t |-" + p1.getinfo (); system. out. println ("\ t |-" + p2.getinfo (); system. out. println ("\ t |-" + p3.getinfo (); system. out. println ("--------------------- modify the original place -----------------------"); // pass the class name. modify the property person in the static property format. borncity = "Beijing"; system. out. println ("\ t |-" + p1.getinfo (); system. out. println ("\ t |-" + p2.getinfo (); system. out. println ("\ t |-" + p3.getinfo () ;}

In the above example,The following method is used to access the static attribute: Class Name. static attribute name, which means that we can directly access the static attribute by class name. Therefore, static attributes are also called class attributes (or class variables ).

2. Declaration and reference of the Static Method

Like static attributes, static modifiers can also be directly accessed by class names. For example, refer to the followingProgramNote:

Class person {string name; int age; static string borncity = ""; // write only one method that can modify the borncity attribute public static void setborncity (string City) {// This is the current object, while the static property belongs to the full class, not an object. Therefore, this keyword cannot be used. // This. borncity = city;} public person (string name, int age) {This. name = Name; this. age = age;} Public String getinfo () {return "name:" + name + ", age:" + age + ", nationality:" + borncity ;}} class oodemo07 {public static void main (string ARGs []) {person p1 = new person ("Shao Yifu", 60); person P2 = new person ("Bao yugang", 61 ); person P3 = new person ("Chiang Kai-shek", 63); system. out. println ("--------------------- -----------------------"); system. out. println ("\ t |-" + p1.getinfo (); system. out. println ("\ t |-" + p2.getinfo (); system. out. println ("\ t |-" + p3.getinfo (); system. out. println ("--------------------- modify the original place -----------------------"); // pass the class name. modify the property // person in the static property format. borncity = "Beijing"; // you can directly use the class name to use the static declaration method. method () format call person. setborncity ("Beijing"); system. out. println ("\ t |-" + p1.getinfo (); system. out. println ("\ t |-" + p2.getinfo (); system. out. println ("\ t |-" + p3.getinfo ());}}

Note that the keyword this cannot be used in the static method. because the static attribute belongs to the entire class, rather than an ongoing instantiated object, this represents the current object.

Next, analyze the access relationship between the static method and the static attribute, and the call relationship between the static method.

2.1 relationship between the static method and the static attribute

Here we need to summarize whether the static method/non-static method can access the relationship between the static property and non-static property:

It can be seen that the static attribute can be accessed by the static method and non-static method, but the non-static attribute can only be accessed by non-static methods, and cannot be accessed by the static method!

2.2 Relationship Between the static method and the static method

Here, you only need to remember one sentence: the static method can only be called directly, otherwise it can only be accessed by instantiating an object (new object. The most typical example here is the call of the main method.We know that the main method is a static method. To call a non-static method in the main method, you must first instantiate the object of the class where the method is located and call the method through the instantiated object. if this method is a static method, the main function can be called directly without instantiating class objects ~

For example, in the following code, the main method can only call the fun () method by instantiating an object (). if fun () is defined as static, no new demo () is required to instantiate the object...

Public class demo09 {// method of the static type can only call the method of the static type public static void main (string ARGs []) {New demo09 (). fun ();} public void fun () {system. out. println ("Hello world !!! ");}}

 

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.