Static and javastatic JAVA members

Source: Internet
Author: User

Static and javastatic JAVA members

Static keywords


I. role: it is a modifier used to modify members (member variables, member methods)
1. Only one member variable is modified by static.
2. When a member is modified as static, an access method is provided. In addition to being called by an object, the member can also be called directly by the class name (class name. static member)


Ii. static features:
1. Loaded with the loading of Classes
2. takes precedence over the existence of Objects
3. shared by all objects
4. It can be called directly by the class name.

Iii. storage location:
1. static member attributes (class variables) exist in the data memory zone as the class is loaded.
2. Attributes of common members exist in heap memory as objects are created.

Iv. lifecycle:
1. Static members (class variables) have the longest life cycle and disappear as the class disappears
2. the life cycle of non-static members (instance variables) is shorter than that of static members, and disappears as the object disappears.

5. Method considerations:
1. Static methods can only access static members.
2. A non-static method can access static members (member attributes, member methods) and non-static members.
3. the keyword "this" and "super" cannot be defined in the method today. Because the static state takes precedence over the object existence, the static state method cannot contain the keyword "this ".

Class G {String name; int age; // member attribute (another name (instance variable) static String country; // static member attribute (another name: class variable) static void jt () {System. out. println (country); // when you access static member attributes in a static method, you cannot use this keyword or access non-static member attributes. } Public class Index {public static void main (String [] args) {g a = new G ();. country = "James"; g B = new G (); System. out. println (. country); // object A, which defines the value of the country member attribute and can be output. This is A conventional System. out. println (B. country); // object B, which does not define the value of the country member attribute. You can also output System. out. println (G. country); // class name. static member. You can also output B. jt (); // call the static method }}

 

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.