instance, local, static variable (Java)

Source: Internet
Author: User

Instance variable: an instance variable if it is declared outside of the class's method code snippet and there is no qualifier static. An instance is another term for an object. Local variable: If declared within the method body of the class, it is a local variable. () static variable: If an instance variable is declared as static, it is a static variable, or a class variable.a static variable can be referenced directly after the class declaration, but the instance variable must be instantiated before it can be used. static variables are neither in the stack nor in the heap, but are placed in a static storage area. (such as the v2=0=200=400 in the book, the last V2 value is the third)static methods and classes occur at the same time,after birth can visit the first born,the class name cannot access the generic member method, nor can the member variable be accessed. Static variables can only be accessed in static methods (because they are born at the same time). By adding the keyword static before the variable declaration, you can leave the local variable with the value of calling a method multiple times. such as: v2=0=200=400, then the result of V2 is 400. static data types
    • Type * Object Name
    • Always store a specific data type
    • The compiler checks for syntax during the encoding phase
JAVAExperiment Five(1) public class MyClass {
private float V1;
private static int v2;public static void main (string[] args) {
MyClass a=new MyClass ();
MyClass b=new MyClass ();
a.v1=100;
a.v2=200;
b.v1=300;
b.v2=400;
System.out.println ("a.v1=" +a.v1);
System.out.println ("a.v2=" +a.v2);
System.out.println ("b.v1=" +b.v1);
System.out.println ("b.v2=" +b.v2);               The result: a.v1=100;               a.v2=400;               b.v1=300; b.v2=400;

instance, local, static variable (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.