Class Constructors in Java Programming

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

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 Source Code for Student.java

 1 package oops;
 2 
 3 public class Student {
 4     String name;
 5     int age;
 6 
 7     public Student(){
 8         System.out.println("Constructor with no parameters called");
 9         name = "noname";
10         age = 1;
11     }
12     
13     public Student(String iname){
14         System.out.println("Constructor with one string parameters called");
15         name = iname;
16         age = 1;
17     }
18     
19     public Student(int iage){
20         System.out.println("Constructor with one integer parameters called");
21         name = "noname";
22         age = iage;
23     }
24     
25     public Student(String iname,int iage){
26         System.out.println("Constructor with two parameters called");
27         name = iname;
28         age = iage;
29     }
30     
31     public void introduce(){
32         System.out.println("name is "+name+" and age is "+age);
33     }
34     
35 }


Source Code for Source Code for Tutorials.java

1 package oops;
2 
3 public class Tutorials {
4     public static void main(String[] args) {
5         Student anil = new Student();  
6         anil.introduce();
7     }
8 }

Watch this video to learn how this works