JAVA Static member Static

Source: Internet
Author: User

Static keyword


One, function: is a modifier, used to decorate members (member variables, member methods)
1, after the static modified member variable only one copy
2, when the member is modified by static, a more access way, in addition to being called by the object can also be called directly by the class name (class name. Static members)


Second, the characteristics of static:
1, loaded with the load of the class
2. Precedence over the existence of objects
3. Shared by all objects
4, can be directly called by the class name

Three, storage location:
1. Static member properties (class variables) exist in the data memory area as the class loads.
2. Normal member properties exist in heap memory as the object is established.

Iv. Life cycle:
1, static members (class variables) the longest life cycle, with the disappearance of the class disappears
2. Non-static members (instance variables) have a shorter life cycle than static members and disappear as the object disappears

Five, the method of attention:
1. Static methods can only access static members
2. Non-static methods that can access static members (member properties, member methods) can also access non-static members
3, today method is not to define the this, super keyword, because static precedence over the existence of the object, so the static method can not appear this

classg{String name; intAge//member properties (another term (instance variable)    StaticString Country;//Static member properties (another term: class variables)    Static voidJT () {System.out.println (country); //The This keyword cannot be used when accessing static member properties inside a static method, and non-static member properties cannot be accessed.     }} Public classindex{ Public Static voidMain (string[] args) {G A=NewG (); A.country= "Xiao Ming"; G B=NewG ();    System.out.println (A.country); //object A, which defines the value of the country member property, can be output, which is the general waySystem.out.println (B.country);//object B, which does not define the value of the country member property, can also outputSystem.out.println (G.country);//the class name, static member, can also outputB.JT (); //calling a static method    }}

JAVA Static member Static

Related Article

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.