The difference between static and non-static variables in Java

Source: Internet
Author: User

There are two types of member variables for the ①java class:

         A variable that is modified by the static keyword, called a class variable or static variable           Another no static modifier, for member variables      ② popular Point said:       The static variables of the    class are only one in memory, and the Java Virtual machine allocates memory for static variables in the process of loading the class, and the static variable is in the method area and is shared by all instances of the class. Static variables can be accessed directly from the class name, depending on the life cycle of the class.          The instance variable depends on the instance of the class. Each time an instance is created, the Java Virtual machine allocates memory for the instance variable once, and the instance variable is in the heap, whose life cycle depends on the life cycle of the instance.   NOTE: The order of initialization in     1.java:         load class;          static variable initialization;         static block; " It can only dispatch static, cannot dispatch non-static "         member variable;          construction method;       2. To show the priority of a static block, I'm going to show a program without the main method, javac it without any errors. public class Test {    static {         System.out.println ("Static");    &Nbsp;    system.exit (0);     }}

The difference between static and non-static variables in Java

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.