Java Static keyword

Source: Internet
Author: User

Static keyword Knowledge point collation


Static Features:

1,static is a modifier that is used to decorate a member.

2,static decorated members are shared by all of the objects.

3,static takes precedence over the existence of an object because static members already exist as the class loads.

4,static decorated members have a way to call, can be directly called by the class name, (class name. static member).

5, statically decorated data is shared data, objects are stored in the unique data.

member variable = = Instance variable

Static variable = = Class variable

The difference between a member variable and a static variable

1, two variables have a different life cycle.

Member variables exist for the creation of objects and are freed as objects are recycled.

Static variables exist as the class loads, disappearing as the class disappears.

2, different calling methods

Member variables can only be called by an object.

A static variable can be called by an object and can also be called by a class name. (It is recommended to call with the class name)

3, different aliases

Member variables are also known as instance variables

Static variables are called class variables

4, the data is stored in different locations.

Member variable data is stored in the object of the heap memory, so it is also called the object's unique data.

Static variable data is stored in the static area of the method area (shared data area), so it is also called the shared data of the object

Considerations for static use

1, static methods can only access static members. (Non-static can access both static and non-static.) )

Cannot make a static reference to the Non-static field name

Non-static variables cannot be referenced in a static method name

2, the This or Super keyword cannot be used in a static method.

3, the main function is static

public class Test {int a =1;public static void Main (String [] args) {new Test (). Show (); public void Show () {System.out.println (a);}}

The main function is special:

1, the format is fixed.

2, recognized and invoked by the JVM



Java Static keyword

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.