Initialize an object in Java
This post will discuss various methods to initialize an object in Java.
1. Naive method
The idea is to get an instance of the class using the new operator and set the values using the class setters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Arrays; class Person { private String name; private int age; @Override public String toString() { return Arrays.asList(name, String.valueOf(age)).toString(); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } // getters } // Initialize an object in Java class Main { public static void main(String[] args) { Person person = new Person(); person.setName("John"); person.setAge(22); System.out.println(person); } } |
Output:
[John, 22]
2. Constructor
When we instantiate an object with a new operator, we must specify a constructor. A constructor has the same name as the class and no return type. It can accept a set of parameters, which are the fields we want to set values for, or it can be parameter-less (no-arg constructor). If we declare a class with no constructors, the compiler will automatically create one for the class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.Arrays; class Person { private String name; private int age; // Constructor public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return Arrays.asList(name, String.valueOf(age)).toString(); } // setters and getters } // Initialize an object in Java class Main { public static void main(String[] args) { Person person = new Person("John", 22); System.out.println(person); } } |
Output:
[John, 22]
3. Copy Constructor
A copy constructor is a special constructor for creating a new object as a copy of an existing object. It takes just one argument that will be another instance of the same class. We can explicitly invoke another constructor from the copy constructor by using the this() statement.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.Arrays; class Person { private String name; private int age; // Constructor public Person(String name, int age) { this.name = name; this.age = age; } // Copy constructor public Person(Person person) { this(person.name, person.age); } @Override public String toString() { return Arrays.asList(name, String.valueOf(age)).toString(); } // setters and getters } // Initialize an object in Java class Main { public static void main(String[] args) { Person oldUser = new Person("John", 22); // Initialize user using the copy constructor Person newUser = new Person(oldUser); System.out.println(newUser); } } |
Output:
[John, 22]
4. Anonymous Inner Class
Another alternative to initialize an object is to use “Double Brace Initialization”. This creates an anonymous inner class with just an instance initializer in it. The use of this method is highly discouraged.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.Arrays; class Person { private String name; private int age; @Override public String toString() { return Arrays.asList(name, String.valueOf(age)).toString(); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } // getters } // Initialize an object in Java class Main { public static void main(String[] args) { // Anonymous class Person person = new Person() {{ // Initializer block setName("John"); setAge(22); }}; System.out.println(person); } } |
Output:
[John, 22]
That’s all about initializing an object in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)