Instance variables are declared in a class, but outside methods, constructors and statement blocks;
When an object is instantiated, the value of each instance variable is determined;
Instance variables are created when the object is created, and when the object is destroyed Destroyed at the time;
The value of the instance variable should be referenced by at least one method, constructor or statement block, so that the outside can obtain the instance variable information through these methods;
null. The value of a variable can be specified at the time of declaration or in the constructor;
variable name. But in static methods and other classes, you should use the fully qualified name: ObejectReference.VariableName.
static keyword## in the class #Declaration, but must be outside the method constructor and statement block.
. Constants refer to variables declared as public/private, final and static types. Constants cannot be changed after initialization.
ClassName.VariableName.
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 Instance methods can operate on the instance variables of the current object or on class variables. Instance methods are called by instance objects.
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);//出错 } } 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();//出错 one.change(101); one.display(); two.change(102); two.display(); } }
cannot appear in class methods.
The above is the detailed content of JAVA classes and objects (the difference between instance variables and class variables) (the difference between instance methods and class methods) explanation. For more information, please follow other related articles on the PHP Chinese website!