"Java Foundation" static keyword related

Source: Internet
Author: User

Static keyword features:
    • Loads as the class loads.
    • Precedence over the existence of an object.
    • shared by all objects
    • Can be called directly by the class name.
Use Note:
    • Static methods can only access static members.
    • But non-static members can access static members.
    • You cannot use the This,super keyword in a static method.
    • The Main method (main) is static.

See above is not want to sleep, simple concept is quite annoying, then let us in the actual code to deepen their cognition.

The invocation of a method can be divided into two types:

    1. Object invocation: Object. Method (Argument)
    2. Class invocation: The name of the class in which this method resides. Method (argument)//The premise is that this method must be modified by static.

A method or field that does not use the static adornment belongs to the object. As an instance member.

Methods or fields that use static adornments belong to the class. As a class member.

The call to them:

If an instance member: can only be called through an object.

If a class member: can be called through the class name, or by an object.

Okay, here's the little program.

1 classDog//A group of dogs2 {3 String name;4     intAge//does not use the static adornment, which belongs to the object, as an instance member. 5     //Dog's name, age, each dog is different, can change randomly. 6 7     Static intnum=100;//use the static modifier, which belongs to the class, to the class member. 8     //and the total number of dogs is constant. 9 }Ten  One classDay5lesson2 A { -      Public Static voidMain (string[] args) -     { theSYSTEM.OUT.PRINTLN ("Call class member by class name--" +dog.num);//compiled by -         //System.out.println ("Invoke instance member by class name--" +dog.name);//compilation does not pass -SYSTEM.OUT.PRINTLN ("Call class member by Object--" +NewDog (). Num);//run through -System.out.println ("Invoke instance member by Object--" +NewDog (). name);//run through +     } -}
View Code

The wrong question hints for the code that is commented out in the above code are as follows:

The above procedure also illustrates the above mentioned: if the instance member: can only be called through the object. If a class member: can be called through the class name, or by an object. However, it is recommended to invoke the class name when invoking a class member, because at the bottom, the object calls the class member and is also converted to the class name.

The following must be mentioned: static zones. It is a storage area in the JVM memory that is dedicated to storing class members.

Let's modify the main method of the previous program:

1 classDog//A group of dogs2 {3 String name;4     intAge//does not use the static adornment, which belongs to the object, as an instance member. 5     //Dog's name, age, each dog is different, can change randomly. 6 7     Static intnum=100;//use the static modifier, which belongs to the class, to the class member. 8     //and the total number of dogs is constant. 9 }Ten classDay5lesson2 One { A      Public Static voidMain (string[] args) -     { -Dog d1=NewDog (); theDog d2=NewDog ();//Declaration of 2 Objects -  -d1.num=111;//modifies a class member with D1.  -System.out.println ("d1.num=" +d1.num); +System.out.println ("d2.num=" +d2.num);//Output -  +d1.age=111;//Modify instance members with D1.  ASystem.out.println ("d1.age=" +d1.age); atSystem.out.println ("d2.age=" +d2.age);//Output -     } -}
View Code

The results are as follows:

Why is it such a result? This is the static decorated field that belongs to a class member and is stored in a dedicated static zone. (PS: Creates space for a class when it is loaded, so it compares consumption performance). The general static method is used in the method of the tool class, such as the Arrays Class (Java.util.Arrays), a large number of static methods can be viewed in the JDK API.

An age (instance member) that is not modified by static does not have this effect. (Ps:int defines a number default value of 0, so d2.age output 0)

"Java Foundation" static keyword related

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.