Class Constructors in Java Programming

In this tutorial, you will learn about class constructors in java programming.

You will learn what are class constructors, how to create and use them, how to create more than one constructor for a class, how do they work in detail with example.

Source code for this video tutorial

Code for Parents.java

package oops;

public class Parents {

  public Parents(){
   
  }
}

Code for Student.java

package oops;

public class Student {
 String name;
 int age;

 public Student(){
  System.out.println("Constructor with no parameters called");
  name = "noname";
  age = 1;
 }
 
 public Student(String iname){
  System.out.println("Constructor with one string parameters called");
  name = iname;
  age = 1;
 }
 
 public Student(int iage){
  System.out.println("Constructor with one integer parameters called");
  name = "noname";
  age = iage;
 }
 
 public Student(String iname,int iage){
  System.out.println("Constructor with two parameters called");
  name = iname;
  age = iage;
 }
 
 public void introduce(){
  System.out.println("name is "+name+" and age is "+age);
 }
 
}

Code for Tutorials.java

package oops;

public class Tutorials {

 public static void main(String[] args) {

  Student anil = new Student(); 
 
  anil.introduce();
 }
}
Back To Top