FuzzDuck
FuzzDuck
Constructor
FuzzDuck
Constructor

Constructor

Inside a class a constructor is set to define the initial parameters when using the new() method to create objects of the class.

Some classes aren’t fully done, unless they have been given specific parameters. The constructor of the class is the area where we define the parameters we want the class to be instantied with.

If, for example, we would like a class Duck to be created with the following parameters int age and string name, we could create a constructor with the following code.

Code Example - Creating class with constructor method
// --- Declares class --- //
public class Duck {

 // --- Declares the variables inside the class --- //
 public int age;
 public String name;

 // --- Start of the constructor method. Defining parameters in parentheses --- //
 public Duck(int newage, String newname) {

  // --- Duck variable "age" is defined by input variable "newage". "name" is defined by "newname".
  age = newage;
  name = newname;

 }

}
//--- Declares class --- //
public class Duck {

 // --- Declares the variables inside the class --- //
 public int age;
 public String name;

 // --- Start of the constructor method. Defining parameters in parentheses --- //
 public Duck(int newage, String newname) {

  // --- Duck variable "age" is defined by input variable "newage". "name" is defined by "newname".
  age = newage;
  name = newname;

 }

}
//--- Declares class --- //
class Duck {

 // --- Declares the variables inside the class --- //
 public $age;
 public $name;

 // --- Start of the constructor method. Defining parameters in parentheses --- //
 function Duck($newage, $newname) {

  // --- Duck variable "age" is defined by input variable "newage". "name" is defined by "newname".
  $this->age = $newage;
  $this->name = $newname;

 }

}
Code Example - Instantiating class with constructor method
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args) {

 // --- Instantiating class using contructor method. Input parameters int newage & String newname --- //
 Duck ducky = new Duck(5, "Ducky");

 // --- Printing ducky's age --- //
 Console.WriteLine(ducky.age);

 // --- Printing ducky's name --- //
 Console.WriteLine(ducky.name);

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String[] args) {

 // --- Instantiating class using contructor method. Input parameters int newage & String newname --- //
 Duck ducky = new Duck(5, "Ducky");

 // --- Printing ducky's age --- //
 System.out.println(ducky.age);

 // --- Printing ducky's name --- //
 System.out.println(ducky.name);

}
// --- Instantiating class using contructor method. Input parameters int newage & String newname --- //
$ducky = new Duck(5, "Ducky");

// --- Printing ducky's age --- //
echo $ducky->age;

// --- Printing ducky's name --- //
echo $ducky->name;