• Summary: Class Variable vs. Instance Variable && Class Method


    这里列举的是一些我平时碰到的一些Java Grammar,日积月累。

    Class Variable vs Instance Variable: 

    Instance variables
    
    Instance variable is the variable declared inside a class, but outside a method Instance variables belong to an instance of a
    class. Another way of saying that is instance variables belong to an object, since an object is an instance of a class. Every object has it’s own copy of the instance variables. Here is what a declaration of an instance variable would look like: Example of an instance variable: class Taxes { int count; /*...*/ }
    Class Variable
    
    Class variables, however, only have one copy of the variable(s) shared with all instances of the class. It’s important to remember that class variables are also known as static member variables in C++, Java, and C#. Each object of the class does not have its own copy of a class variable. Instead, every object shares the one and only copy of that class variable – and any changes made to that copy are seen by all of the objects of that class. Here is what a class variable – or a static member variable – would look like in C++:
    
    Example of a class variable:
    
    class Taxes
    {
      static int count;
      /*...*/
    }
    Difference between class and instance variables
    
    Now, it should be clear what the difference between instance and class variables is. Class variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.
    Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
    For example, suppose you want to create a number of Bicycle objects and assign each a serial number, beginning with 1 for the first object. This ID number is unique to each object and is therefore an instance variable. At the same time, you need a field to keep track of how many Bicycle objects have been created so that you know what ID to assign to the next one. Such a field is not related to any individual object, but to the class as a whole. For this you need a class variable, numberOfBicycles, as follows:
     1 public class Bicycle {
     2         
     3     private int cadence;
     4     private int gear;
     5     private int speed;
     6         
     7     // add an instance variable for the object ID
     8     private int id;
     9     
    10     // add a class variable for the
    11     // number of Bicycle objects instantiated
    12     private static int numberOfBicycles = 0;
    13         ...
    14 }
    Class variables are referenced by the class name itself, as in
    
    Bicycle.numberOfBicycles

    You can use the Bicycle constructor to set the id instance variable and increment the numberOfBicycles class variable:
     1 public class Bicycle {
     2         
     3     private int cadence;
     4     private int gear;
     5     private int speed;
     6     private int id;
     7     private static int numberOfBicycles = 0;
     8         
     9     public Bicycle(int startCadence, int startSpeed, int startGear){
    10         gear = startGear;
    11         cadence = startCadence;
    12         speed = startSpeed;
    13 
    14         // increment number of Bicycles
    15         // and assign ID number
    16         id = ++numberOfBicycles;
    17     }
    18 
    19     // new method to return the ID instance variable
    20     public int getID() {
    21         return id;
    22     }
    23         ...
    24 }

    Class Methods

    The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in

    ClassName.methodName(args)
    

    Note: You can also refer to static methods with an object reference like
    instanceName.methodName(args)
    
    but this is discouraged because it does not make it clear that they are class methods.

    A common use for static methods is to access static fields. For example, we could add a static method to the Bicycle class to access the numberOfBicycles static field:

    public static int getNumberOfBicycles() {
        return numberOfBicycles;
    }
    

    Not all combinations of instance and class variables and methods are allowed:

    • Instance methods can access instance variables and instance methods directly.
    • Instance methods can access class variables and class methods directly.
    • Class methods can access class variables and class methods directly.
    • Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
  • 相关阅读:
    信息的表示和处理
    Linux基础与Linux下C语言编程基础
    Linux基础入门
    第4次实验
    第三次实验
    第二次实验
    java第一次实验
    数据库提示日志文件不可用
    SQL Server 2012 列存储索引分析(翻译)
    修改delphi xe6 FMX Label字体颜色
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4114979.html
Copyright © 2020-2023  润新知