Crazy Java Learning Notes Object-oriented-member variables and local variables

Source: Internet
Author: User

Java variables are categorized by their scope: member variables and local variables. Note: There is no concept of global variables in Java

First, member variables:

A member variable is a variable defined in a class , which can be divided into class and instance variables-- with or without static modification

Characteristics of Instance variables:

No memory space is allocated until the instance is created;

Starting with an instance, the instance variable exists;

Instance is destroyed, the instance variable does not exist;

0~n instance variables, each time a program creates an instance, the system allocates a piece of memory for the instance

Characteristics of class variables:

Starting with a class, a class variable exists, and the class information in a JVM is just one.

Each JVM loads at most one class at a time, and the system allocates a piece of memory for that class. (class variables are loaded when the class is loaded or when the object is created)

Member Variable rules:

default initialization is performed for member variables: primitive Type initial value is 0, 0.0, \u00000, FALSE, reference type initial value is null

Note the point : when a program accesses a class variable through an instance, because the class variable itself is not part of the instance, the underlying is actually delegated to be accessed through the class.

1 class var{2    String name; 3     int Age ;        Instance variable 4     static  int  num;//class variable 5 }
1  Public classtestvar{2      Public Static voidMain (string[] args) {3         //Var v;//compile times v variable not initialized4         //Var v = null;//when V is null, the call to NUM will report a null pointer exception5Var v =NewVar ();//instance variables must be initialized by the programmer6 System.out.println (v.num);7 8Var v2 =NewVar ();9V.age = 11;Ten System.out.println (v2.age); OneV.num = 99; A System.out.println (V.num);//→system.out.println (Var.num); when a program accesses a class variable through an instance, it is actually accessed through a class  . -     } -}

Second, local variables:

Local variables are stored in the appropriate method stack.

Local variable rules:

Local variables must be assigned by the programmer , and the system will not initialize the local variables by default

Local variables are never allowed to have the same name. Within a method, the variable name cannot be the same as the variable name of the code block in the method, but a local variable with the same name is allowed in two code blocks

The local variable is valid only within the code block, the code block is invalid, and the formal parameter is valid within the current method.

1  Public classLocalVar {2      Public voidinfo (String name) {3String s = "haha";4             //String name = "haha"; Compile error method The name of the internal argument cannot be the same as the formal parameter5             {6                 //String s = "Test";//in the same method, the variable name in the method block cannot be the same as the variable in the method7String S1 = "Test";8 System.out.println (S1);9             }Ten             { OneString S1 = "Localtest"; A System.out.println (S1); -             } -     } the  -      Public Static voidMain (string[] args) { -String s = "Test";//method names can be the same in different methods-because local variables are only valid within a method -LocalVar v2 =NewLocalVar (); +V2.info ("AVV"); -     } +}

Crazy Java Learning Notes Object-oriented-member variables and local variables

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.