A class is a blueprint for Object, you can create an object from a class. Let's take Student class and try to create a Java object for it.
How to Declare, Create and Initialize an Object in Java
Let's create a simple Student class which has name and college fields. Let's write a program to declare, create and initialize a Student object in Java.
package net.javaguides.corejava.oops;
public class Student {
private String name;
private String college;
public Student(String name, String college) {
super();
this.name = name;
this.college = college;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public static void main(String[] args) {
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
}
}
From the above program, the Student objects are:
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
Each of these statements has three parts (discussed in detail below):
Declaration: The code Student student; declarations that associate a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates the object.
Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
Declaring a Variable to Refer to an Object
General syntax:
type name;
This notifies the compiler that you will use a name to refer to data whose type is a type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable.
From the above program, we can declare variables to refer to an object as:
Student student;
Student student2;
Student student3;
Instantiating a Class
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.
For example:
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
Note that we have used a new keyword to create Student objects.
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
Initializing an Object
The new keyword is followed by a call to a constructor, which initializes the new object. For example:
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
From the above code will call below constructor in Student class.
public class Student {
private String name;
private String college;
public Student(String name, String college) {
super();
this.name = name;
this.college = college;
}
}
Reference
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course