Static variables and instance variables of Java classes

Source: Internet
Author: User

There are two types of class member variables: one is a variable modified by the static keyword, called a class variable or a static variable, and the other is a variable not modified by the static keyword, it is called an instance variable.

 

The difference between static variables and instance variables is:

1. There is only one static variable of the class in the memory. the Java Virtual Machine allocates memory for the static variable during the class loading process. The static variable is located in the metadata method and is shared by all instances of the class.

Static variables can be accessed directly by class names. The lifecycle of a static variable depends on the lifecycle of the class. When a class is loaded, the static variable is created and allocated memory,

When the class is detached, the static variable is destroyed and the occupied memory is revoked.

 

2. Each instance of the class has corresponding instance variables. Each time a class instance is created, the Java Virtual Machine allocates memory for the instance variable once, And the instance variable is located in the heap area.

The life cycle of instance variables depends on the life cycle of the instance. When an instance is created, the instance variables are created and allocated with memory. When the instance is destroyed, the instance variables

Memory occupied by destruction and revocation.

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.