Home  >  Article  >  Java  >  JAVA classes and objects (the difference between instance variables and class variables) (the difference between instance methods and class methods) explanation

JAVA classes and objects (the difference between instance variables and class variables) (the difference between instance methods and class methods) explanation

高洛峰
高洛峰Original
2018-05-25 09:30:004844browse

This articleIntroductionJAVAClasses and objects(The difference between instancevariablesand class variables)(The difference between instance methods and class methods)Explanation

Instance variables

Class variables (static variables)

  • Class variables are also called static variables, and are represented by the

    static keyword## in the class #Declaration, but must be outside the method constructor and statement block.

  • No matter how many objects a class creates, the class only has one copy of the class variable.
  • Static variables are rarely used except when declared as
  • constants

    . Constants refer to variables declared as public/private, final and static types. Constants cannot be changed after initialization.

  • Static variables are stored in the static storage area. Often declared as constants, variables are rarely declared using static alone.
  • Static variables are created at the beginning of the program and destroyed at the end of the program.
  • Have similar visibility to instance variables. But in order to be visible to users of the class, most static variables are declared as public types.
  • Default values ​​are similar to instance variables. The default value of numeric variables is 0, the default value of Boolean variables is false, and the default value of reference types is null. The value of a variable can be specified when declaring it or in the constructor. In addition, static variables can also be initialized in static statement blocks.
  • Static variables can be accessed through:
  • ClassName.VariableName.

  • When a class variable is declared as a public static final type, the class variable name must use uppercase letters. If the static variable is not of public or final type, its naming method is consistent with the naming method of instance variables and local variables.
  • 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);
        }
    }

这篇文章介绍JAVA类与对象(实例变量与类变量的区别)(实例方法和类方法的区别 )说明

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 methods cannot access instance variables, only class variables. Class methods are called by the class name or instance object. This or
  • super

    keywords

    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn