Variables in Java

Source: Internet
Author: User

The variables in Java are divided into the following three types:
    • Local variables
    • Member variables
    • class variables (static variables)

    • Local variables
      1. Local variables are allocated in-memory stack space.
      2. Local variables do not have default values, so they must be initialized first if they are to be used.
      3. Local variables are declared in methods, construction methods, and code blocks (code blocks are blocks of code in curly braces, variables declared in curly braces are local variables, and their scope is within the range of curly braces, and local variables are destroyed automatically once the code block statement executes).
      4. Local variables cannot be modified by the access modifier.
    • Member variables

Member variables are also called global variables, which include instance variables, class variables, and constants. But in my understanding, it is generally used to refer to the example variables are mostly, instance variables like the following several characteristics:

      1. Instance variables are declared in a class, but beyond methods and construction methods, the scope is the entire class.
      2. The instance variable is created when the object is created, destroyed as the object is destroyed, and its memory is allocated in the heap space.
      3. Instance variables can be declared before they are used, or they can be declared after use.
      4. Instance variables can be decorated with access modifiers.
      5. Instance variables have default values, the default value type is 0, the default for Boolean type is false, and the reference type defaults to NULL.
      6. When instantiating an object, the value of the instance variable is also determined.
    • class variables

A class variable is also called a static variable, often modified with static (my understanding is that the static variable is the variable that is modified with static).

      1. Static variables are created at the beginning of the program and destroyed at the end of the program.
      2. No matter how many objects a class creates, static variables have only one copy, that is, static variables in different objects are actually pointing to the same one.
      3. Class variable names are generally recommended to use uppercase when the class variable is declared as public static final type. Other normal naming.
      4. The default value is the same as the instance variable.
      5. Static variables can be accessed directly with the class name plus the variable name, which is Classname.var, but the instance variable must be accessed through the instance object.
      6. Non-static members cannot be accessed in a static method.

Public  class  test{        public  int  A;     Instance variable        public  static  int  B;    Static variable        public  void  C () {        int  D;  Local variable        }}

Variables in 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.