Java Constructor Example (with video) (original) (raw)

In this example, we are going to explain the use of the Java constructor for java classes.

In most cases, we need to create an object of a class, so as to use it. This can be done by using the constructors that are provided within the class, which will create a new object of it and also assign initial values to its fields.

You can also check this tutorial in the following video:

Java Constructor Example – Video

1. Calling Constructor

We can create an object, simply by calling a constructor with the use of the operator new: Person personObject = new Person();

Java Constructor

This line of code creates a new object of the class Person.

2. Declaring Constructors

Constructors have the same name as the class and they have no return type, since they return an instance of the class. We can use the access modifier, which can be public, protected, private or “default”, so as to manage the accessibility of other classes to it.

Moreover constructors may, or may not have any arguments. A no-argument constructor is used to initialize the fields with default, predefined values.

We can have more than one constructors, depending on the number and type of arguments which they require in their declaration.

Tip
We are not allowed to create more than one constructors with the same number and type of arguments for each class, since the compiler cannot distinguish them and a compilation error will occur.

Let’s have a look at the following example:

Person.java:

0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344 package com.javacodegeeks.core.constructor;public class Person { private String name; private int age; public Person() { this.name = "Unknown"; this.age = 0; } public Person(String Name) { this.name = Name; this.age = 0; } public Person(String Name, int Age) { this.name = Name; this.age = Age; } public String getName() { return name; } public int getAge() { return age; } public static void main(String args[]) { Person person1 = new Person(); Person person2 = new Person("Jim"); Person person3 = new Person("John", 28); System.out.println("person1: " + person1.getName() +" is "+ person1.getAge() + " years old."); System.out.println("person2: " + person2.getName() +" is "+ person2.getAge() + " years old."); System.out.println("person3: " + person3.getName() +" is "+ person3.getAge() + " years old."); }}

Output:

123 person1: Unknown is 0 years old.person2: Jim is 0 years old.person3: John is 28 years old.

3. Declaring Constructor in subclass

The previous example demonstrated the use of constractors withing a single class. When it comes to class Inheritance, we can call the constructor of the superclass, within the subclass by using super.

The following simple example illustrates how we can declare the constractor of a subclass. In this case, class Employee extends Person by adding the job of the person as extra information.

Employee.java:

010203040506070809101112131415161718192021222324252627282930313233343536373839 package com.javacodegeeks.core.constructor;public class Employee extends Person { private String job; public Employee() { super(); this.job = "Unknown"; } public Employee(String Name) { super(Name); this.job = "Unknown"; } public Employee(String Name, int Age, String Job) { super(Name, Age); this.job = Job; } private String getJob() { return job; } public static void main(String args[]) { Employee employee1 = new Employee(); Employee employee2 = new Employee("Tom"); Employee employee3 = new Employee("George", 40, "Teacher"); System.out.println("employee1: " + employee1.getName() + " is " + employee1.getAge() + " years old " + employee1.getJob()); System.out.println("employee2: " + employee2.getName() + " is " + employee2.getAge() + " years old " + employee2.getJob()); System.out.println("employee3: " + employee3.getName() + " is " + employee3.getAge() + " years old " + employee3.getJob()); }}

Output:

123 employee1: Unknown is 0 years old Unknownemployee2: Tom is 0 years old Unknownemployee3: George is 40 years old Teacher

4. The Default Constructor

In case we do not provide a constructor for our class at all, the java compiler provides us with a default constructor. This default constructor is not visible in our code and has no input arguments.

The following class has no constructor. We create an object of it and we print the values of its two variables. Notice that no error is generated and the variables have been initialized. This is due to the utilization of the default constructor.

DefaultConstructorExample.java:

010203040506070809101112131415161718192021 package com.javacodegeeks.core.constructor;public class DefaultConstructorExample { private int num; private String str; public int getNum() { return num; } public String getStr() { return str; } public static void main(String args[]) { DefaultConstructorExample obj = new DefaultConstructorExample(); System.out.println("num = " + obj.getNum()); System.out.println("str = " + obj.getStr()); }}

Output:

6. Download the Source Code

This was an example of using Constructors in Java.

Last updated on May 05th, 2021

Photo of Xenofon Dimitroulis

Xenofon has graduated from the Department of Informatics at Athens University of Economic and Business. His studies were focused in the area of Information Management, as well as Computer Systems and Networks. He has successfully developed a series of projects varying form simple desktop, web and mobile applications, scaling to more complex which included AI algorithm implementations and Multithreaded Distributed Systems.