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

Leave a Reply

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

Latest Post

  • 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
  • Member variables in Java
    Java’s member variables are all declared outside the methods in the class and represent the properties that the object can have. Member variables consist of