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