Static variables (class variables), instance variables, local variables, and member variables in Java

Source: Internet
Author: User

Guide

In the process of learning Java, it is easy to get dizzy from these various variables at first, this blog mainly introduces the relationship between these variables and the difference.

Routines
Package Com.cunyu.demopublic class Demo {private String name;  Member variable, instance variable private int age;  Member variable, instance variable private int ID;  Member variable, instance variable public static final String school = "Kassel College";    Member variable, static variable (class variable) public static String level = "SSS";    Member variable, static variable (class variable) public int getage () {return age;    } public int getId () {return ID;    } public String GetName () {return name;    public void Setage (int.) {this.age = age;    } public void SetId (int ID) {this.id = ID;    } public void SetName (String name) {this.name = name;  } public void Study () {String subject1 = "Dragon Slayer"; Local variable String subject2 = "Alchemy";        Local variable SYSTEM.OUT.PRINTLN ("Learning subjects:" + Subject1 + "," + subject2);        } public static void Main (string[] args) {Demo demo = new Demo ();        Demo.setage (23);        Demo.setid (14000001);        Demo.setname ("Chu Zi Hang"); System.out.println ("ID:" + demo.getid () + "Age: "+ demo.getage () +" Name: "+ demo.getname ());        System.out.print ("Major subject:");        Demo.study ();        System.out.println ("College:" + Demo.school);    System.out.println ("rank:" + demo.level); }}
Relation and difference of each variable
    • Member variables: The scope is the entire class, equivalent to a global variable in C, defined outside the method body and the statement block, generally defined under the declaration of the class, the member variables include instance variables and static variables (class variables);
    • Instance variables: Independent of variables other than methods, no static adornments, declared in a class, but outside of methods, construction methods, and statement blocks, numeric variables default to 0, Boolean defaults to False, reference type default value is null;
    • Static variables (class variables): Variables independent of the method, with static decoration, the default value is similar to the instance variable, only one copy of a class, belonging to the object is common, stored in the static storage, is often declared as a constant, the call is generally the class name. Static variable name, can also be called by the name of the object.
    • Local variables: Variables in the method of a class, access modifiers cannot be used for local variables, declared in methods, construction methods, or block of statements, allocated on the stack, no default value, must be initialized;

Static variables (class variables), instance variables, local variables, and member 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.