JAVA class and object --- differences between instance variables and class variables, differences between instance methods and class methods, and java ---

Source: Internet
Author: User

JAVA class and object --- differences between instance variables and class variables, differences between instance methods and class methods, and java ---
Instance variables

  • Instance variables are declared in a class, but outside of methods, constructor methods, and statement blocks;
  • After an object is instantiated, the value of each instance variable is determined;
  • Instance variables are created when an object is created and destroyed when the object is destroyed;
  • The instance variable value should be referenced by at least one method, constructor, or statement block, so that the external can obtain instance variable information through these methods;
  • Instance variables are visible to methods, constructor methods, or statement blocks in the class. Generally, the instance variables should be set to private. You can use an access modifier to make instance variables visible to sub-classes.
  • Instance variables have default values. The default value of the numeric variable is 0, the default value of the Boolean variable is false, and the default value of the referenced type variable is null. Variable values can be specified during Declaration or in constructor;
  • Instance variables can be accessed directly through the variable name. However, in static methods and other classes, you should use the fully qualified name: ObejectReference. VariableName.
Class variable (static variable)
  • Class variables are also known as static variables. They are declared with the static keyword in the class, but must be outside the method constructor and statement block.
  • No matter how many objects a class creates, the class only has a copy of the class variables.
  • Static variables are rarely used except declared as constants. Constants are variables declared as public/private, final, and static. Constants cannot be changed after initialization.
  • Static variables are stored in the static storage area. It is often declared as a constant and rarely declares variables independently using static.
  • Static variables are created at the beginning of the program and destroyed at the end of the program.
  • Similar to instance variables. To be visible to users of the class, most static variables are declared as public.
  • The default value is similar to the instance variable. The default value of the numeric variable is 0, the default value of the Boolean variable is false, and the default value of the reference type is null. The variable value can be specified during Declaration or in the constructor. In addition, static variables can be initialized in static statement blocks.
  • You can use the following static variables:ClassName. VariableName.
  • When a class variable is declared as public static final, the class variable name must use uppercase letters. If the static variables are not of the public and final types, they are named in the same way as the instance variables and local variables.
  •  1 class TiXing{ 2     float up,height; 3     static float down; 4      5     TiXing(float x,float y,float z){ 6         up=x; 7         height=y; 8         down=z; 9     }10 }11 12 public class ep3_9{13     public static void main(String args[]){14         TiXing one=new TiXing(1,2,3);15         System.out.println("one's down is:"+one.down);16         TiXing two=new TiXing(4,5,6);17         System.out.println("one's down is:"+one.down);18         System.out.println("two's down is:"+two.down);19     20         System.out.println("TiXing's down is:"+TiXing.down);21     }22 }
    View Code

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.
1 class TiXing {2 private float up, height; 3 private static float down; 4 5 TiXing (float x, float y, float z) {6 up = x; 7 height = y; 8 down = z; 9} 10 public void display () {11 System. out. println ("up is:" + up + "height is:" + height + "down is:" + down); 12} 13 public static void change (float number) {14 down = number; 15 // System. out. println ("height:" + height); // error 16} 17} 18 19 public class ep3_9 {20 public static void main (String args []) {21 TiXing one = new TiXing (1, 2, 3); 22 one. display (); 23 TiXing two = new TiXing (4,5, 6); 24 one. display (); 25 two. display (); 26 27 // TiXing. display (); // error 28 one. change (101); 29 one. display (); 30 two. change (102); 31 two. display (); 32} 33}
View Code

Note: This 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.