static properties and static methods of Java

Source: Internet
Author: User
Tags modifier

Objective

Static properties and methods must use the static modifier

The difference between static and non-static properties:

1, in memory location different all properties with static modifier or methods are stored in the memory of the method area rather than the static property in the memory of the heap area

2. When there is a different static property or method exists before the object is created and not a static property exists after the object is created

3. A static property is shared among all objects in a class

4. Life cycle different static properties are destroyed after the class disappears and the non-crystalline attribute is destroyed after the object is destroyed

5. Usage:

A. Static properties can directly access non-static properties through the class name cannot be accessed directly through the class only through object access

B. The same point is that both can be used after the object is created to use the following to see a static property is a shared example of all objects in a class The final result is 20.

Class Beauty{int age1;static int age2;} Class Testperson{public static void Main (string[] args) {Beauty B1 = new Beauty (); b1.age2 = 10; Beauty b2 = new Beauty (); b2.age2 = 20; System.out.println (B1.age2); System.out.println (B2.age2); System.out.print ("\ n");}}
Static properties in-memory distribution map:

Considerations for using static:

1. Methods with static modifiers can only access static properties

2. Non-static methods that can access static properties can also access non-static properties

3. Static variables cannot be defined by non-static methods

4. The This keyword cannot be used in static methods

5. Static methods cannot invoke non-static methods, but can

static properties and static methods of Java

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.