Class Variables

Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes.

  • Object references and object attribute variables are created for each object as the object is created. However, class variables are created only once, regardless of the number of objects created from the class.
  • All objects created from one class share class variables.
  • Use class variables to communicate between objects or to express common properties of objects. Unlike object variables, class variables are accessed through the class name.
class Box {
  int width;
  int height;
  int depth;
  long idNum;
  //Class variable declaration
  static long boxID = 0;
  public Box() {
    //Increase the value of the class variable each time the constructor is executed
    //The idNum value of all objects created from the Box class will have a unique value depending on the order in which the objects are created.
    idNum = boxID++;
  }
}
Basic example of class variable 1
class Box {
  int width;
  int height;
  int depth;
  long idNum;
  static long boxID = 0;
  public Box() {
    idNum = boxID++;
  }
}

class StaticDemo {
  public static void main(String args[]) {
    Box mybox1 = new Box();
    Box mybox2 = new Box();
    Box mybox3 = new Box();
    Box mybox4 = new Box();
    System.out.println("id number of mybox1 : " + mybox1.idNum);
    System.out.println("id number of mybox2 : " + mybox2.idNum);
    System.out.println("id number of mybox3 : " + mybox3.idNum);
    System.out.println("id number of mybox4 : " + mybox4.idNum);
    System.out.println("The total number of boxes is " + Box.boxID);
  }
}
Basic example of class variable 2
public class StaticDemo1 {
  public static void main(String args[]) {
    int objectcount = Integer.parseInt(args[0]);
    //Array object array of Instance class type objectArray [] generated
    Instance objectArray[] = new Instance[objectcount];
    for(int i=0; i<objectcount; i++) {
      System.out.println(Instance.count + "번째 객체 생성") ;
    }
    System.out.println("The number of objects created is " +  Instance.count);
  }
}

class Instance {
  static int count = 0;
  Instance() {
    count++;
  }
}
Basic example of class variable 3

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post

  • Mastering the daily conversation in 4 languagesBasic sentences expressing my mood – I’m
  • 8 things you need to think about for work-life balance
    View on Youtube Image: juliezhuo.com who works as a design VP for Facebook, presented 8 ways for work and life balance in her article “The
  • 10 special abilities of Smartphone
    View on Youtube No matter where you go or what you do, your smartphone is always with us. We use the internet with a smartphone,
  • Java: abstract class, abstract method
    An abstract class in Java is a class that declares an abstract function to be implemented in a subclass as an abstract method. An abstract
  • Java: inheritance and constructors
    When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when
  • Java reserved words: super, final
    super is a Java reserved word that is used to access member variables or methods of a superclass that is obscured by a subclass. Access
  • Java: finalize method and garbage collection
    The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls
  • Java: Method overloading
    Method overloading is the same concept as constructor overloading. That is, you can use methods of the same name in the same class, but methods
  • Java final variable
    You can specify an end variable using the reserved word final. The final variable represents a constant value that can not be changed. Typically, final
  • Java class variables
    Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes. Object references and object