Java: constructor, constructor overloading, reserved words

Constructor

Java’s Constructor is a special method that writes the initialization process when an object is created from a class.

The name of the constructor is the same as the name of the class, and this special method is supposed to be executed automatically once when the object is created.

Constructors can also have access restrictions, like member variables, and are equivalent to member variable access restrictions. If the constructor is declared private, it means it is used only inside the class.

[public/protected/private] ClassName(parameter) {
  ... //Initialization sentence
}
//Encapsulation class
class Box {
  //Declare variable as private to prevent access from outside
  //Providing information concealment
  private int width;
  private int height;
  private int depth;
  private int vol;

  //Declare a constructor with the same name as the class
  public Box(int a, int b, int c) {

    //Perform initialization
    width = a;
    height = b;
    depth = c;

  }
  public int volume() {
    vol = width * height * depth;
    return vol;
  }
}

class BoxTestDemo {
  public static void main(String args[]) {
    int vol;
    Box mybox1 = new Box(10, 20, 30);
    vol = mybox1.volume();
    System.out.println("The volume of the mybox1 object : " + vol);
  }
}
Basic example of constructor method

Constructor overloading

Java can be used to nest one or more constructors in a class. If you use multiple constructors, the names of the constructors may be the same, but the type and number of parameters must be different.

class Box {
  int width;
  int height;
  int depth;

  //Four constructors have different parameters
  public Box() {
    width = 1;
    height = 1;
    depth = 1;
  }
  public Box(int w) {
    width = w;
    height = 1;
    depth = 1;
  }
  public Box(int w, int h) {
    width = w;
    height = h;
    depth = 1;
  }
  public Box(int w, int h, int d) {
    width = w;
    height = h;
    depth = d;
  }
}

//Create class
//Automatic matching based on parameters
Box mybox1 = new Box();
Box mybox2 = new Box(5);
Box mybox3 = new Box(5, 10);
Box mybox4 = new Box(5, 10, 15);
Basic example of constructor overloading 1
class Box {
  int width, height, depth;
  double dwidth, dheight, ddepth;
  public Box(int w, int h, int d) {
    width = w;
    height = h;
    depth = d;
  }
  public Box(double w, double h, double d) {
    dwidth = w;
    dheight = h;
    ddepth = d;

  }
}

//Create class
//Automatic matching based on parameters
Box mybox1 = new Box(5, 10, 15);
Box mybox2 = new Box(20.3, 10.7, 15.23);
Basic example of constructor overloading 2

Reserved words “this”

In Java, this refers to the current object. Used when the constructor or method parameters and object variables have the same name, or when calling other constructors within the same class.

class Box {
  int width;
  int height;
  int depth;

  public void Box(int width, int height, int depth) {
    //this.width means the object property variable "width" of the current object
    //This can be used as the name of an object variable and constructor parameter with the same name
    this.width = width;
    this.height = height;
    this.depth = depth;
  }
}
this Basic example 1
//'this' class will eventually call the final constructor regardless of the object's type. 'this' serves to call another constructor within the same class.
class Box {
  int width;
  int height;
  int depth;
  public Box() {
    this(1, 1, 1);
  }
  public Box(int w) {
    this(w, 1, 1);
  }
  public Box(int w, int h) {
    this(w, h, 1);
  }
  public Box(int w, int h, int d) {
    width = w;
    height = h;
    depth = d;
  }
}
this Basic example 2

Share This Post

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

Latest Post