JAVA class and object (6) ------ differences between instance variables and class variables, differences between instance methods and class methods, and java ------
Instance variable class variable (static variable)
class TiXing{ float up,height; static float down; TiXing(float x,float y,float z){ up=x; height=y; down=z; }}public class ep3_9{ public static void main(String args[]){ TiXing one=new TiXing(1,2,3); System.out.println("one's down is:"+one.down); TiXing two=new TiXing(4,5,6); System.out.println("one's down is:"+one.down); System.out.println("two's down is:"+two.down); System.out.println("TiXing's down is:"+TiXing.down); }}
Instance method and class method access to instance variables and class variables
The instance method can operate the instance variables of the current object, or the class variables. The instance method is called by the instance object.
Class methods cannot access instance variables. They can only be class variables. The class method is called by the class name or instance object. This or super keyword cannot appear in class methods.
Class TiXing {private float up, height; private static float down; TiXing (float x, float y, float z) {up = x; height = y; down = z ;} public void display () {System. out. println ("up is:" + up + "height is:" + height + "down is:" + down);} public static void change (float number) {down = number; // System. out. println ("height:" + height); // error} public class ep3_9 {public static void main (String args []) {TiXing one = new TiXing (1, 2, 3); one. display (); TiXing two = new TiXing (4,5, 6); one. display (); two. display (); // TiXing. display (); // error one. change (101); one. display (); two. change (102); two. display ();}}