Learn how to initialize an object of a class in java through constructor with simple example and source code.
what will you learn?
- What is object initialization?
- How to initialize an object and why?
- Object initialization by hard code value in constructor
- Object initialization by constructor parameter
Prerequisite: You need to be familiar with below concept
- Java Constructor
- How to create an object of a class in java program?
- Constructor with parameter in java
Object initialization in Java
First, let us understand what does it mean by object initialization of a class.
Object initialization means, initializing class fields. Suppose there is a Person class having a field “name”. When we create objects like Peter , John and Linda etc. of the class Person, the name field should contain their name. So for good?
So, how do we initialize class fields?
We can initialize class fields in a constructor of a class. In the constructor ,we can write hard code value like name =”Peter”; as simple as that. Also, earthier you hard code values of all fields or receive values in the constructor parameter and initialize. Difficult, don’t worry, we will understand by example.
First lets see the output of a simple class when we don’t initialize an object of the class. Below is the class Person having a field name, that is not initialized in constructor. On printing the name filed using method getName, the person name will be “null”
See the output of the program below
class Person {
String name;
// Constructor
public Person() {
}
public String getName() {
return this.name;
}
}
public class TestPerson {
public static void main(String[] args) {
Person john = new Person();
john.getName();
System.out.println("Person:" + john.getName());
}
}
Output:
Person:null
So, if we don’t initialize an object , we can not have some valid name of the objects like Peter and John etc. We need to provide some valid name to them. In fact, Every person in the world is having his name? Right? So, why not it programming.
Now, see the example, where we have initialized field “name” in the constructor of the Person class as “this.name = “Peter”;”
Output will be now as : Person:Peter
class Person {
String name;
// Constructor
public Person() {
this.name = "Peter";
}
public String getName() {
return this.name;
}
}
So, for you understand what is object initialization and why to initialize them.
So for, we learned,
We can initialize object of a class in constructor. Either by hard code values or by receiving values in constructor parameter. We saw how we can do hard code initialization.
Now, Let us learn, how we can initialize objects thorough constructor parameter.
Object initialization through parameterized constructor in java
We need to create a parameterized constructor in the class that can accept arguments. The class fields will be initialized with these parameters in the constructor.
Recommend to read here how to use parameter in constructor in java and how it is called
When you create an object with arguments, the class constructor in java program will be automatically called and fields will be initialized.
Let’s understand object initialization by an example,
Consider a class Person that has 3 fields i.e. name, age and gender. We will create a constructor with 3 parameters as below and initialize the class members with these parameters.
Here is an example of constructor with 3 parameters. All fields are initialized into it. Note that “this” keyword before fields has been used, so, we can know that they are class fields. In fact, to differentiate parameters and class fields.
public Person(String name, int age, String gender){
this.name = name;
this.Age = age;
this.gender = gender;
}
When we create object of the class as below, the above constructor will be called automatically, and the class members will be initialized.
Person John = new Person(“John”,30,”M”);
Code – How to initialize an object through constructor in java
class Person {
// properties of a person
private String name;
private int Age;
private String gender;
// Constructor : initialize all class fields
public Person(String name, int age, String gender) {
this.name = name;
this.Age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return Age;
}
public String getGender() {
return gender;
}
}
/*
* Test objects initialization.
*/
public class Sample {
public static void main(String[] args) {
Person scott = new Person("Scott", 30, "M");
System.out.println("Name: " + scott.getName());
System.out.println("Age: " + scott.getAge());
System.out.println("Gender: " + scott.getName());
}
}
Output:
Name: Scott
Age: 30
Gender: Scott