A class is like a blueprint for an object. It defines what attributes (fields) and behaviors (methods) an object will have. Before creating an object, the class is defined with these fields and methods, giving structure to what objects based on this class will look like.
An object is an instance of a class. Each object has its own set of data (attributes) and behaviors (methods) defined by the class. Importantly, if we change the data of one object, it doesn’t affect the data of other objects created from the same class; each object maintains its own state.
We have already discussed classes and objects in Java separately. If you want to learn more, then the links are below;
What Is Class In Java With Example?
In this article, we understand both class and object in Java and how both work together.
Create the class
Let’s create a simple class called Person, which has two attributes: name and age, and a method to display the information.
class Person {
// Attributes (fields or variables)
String name;
int age;
// Method to display person's information
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
While a class defines the structure, an object is an actual entity created from that class, holding real values for its attributes and able to perform actions (methods) defined in the class.
For example, if the Person class is the blueprint, then an object is an actual person (like “Alice”) created based on that blueprint.
Create an object
ClassName objectName = new ClassName();
ClassName is the name of the class and objectName is the name of the object. A new keyword is used for making an object and ClassName() is the constructor calling.
How do you access object properties in Java?
We have two object properties: fields and methods. We can access fields and methods inside the class and outside the class.
To access properties within the class, we can refer to them directly by their names. For example, if we have a Person class with fields like name and age and a method called displayInfo(), we can access name and age directly within displayInfo() without any additional syntax.
However, to access these properties from outside the Person class, we use the dot (.) operator, also known as the “object member access operator.”
Below is an example of how to access fields and methods using the dot operator, assuming we have a Person class with the fields name, age, and the method displayInfo():
public class Main {
public static void main(String[] args) {
// Creating the first object of the Person class
Person obj= new Person();
obj.name="John";
obj.age=26;
obj.displayData();
}
}The above example shows only how to access object properties outside the class in Java. We will make a whole program also for this.
If you want to print the values of the Object field (name, age) outside the class without using displayData() method then you can use the below code:
public class Main {
public static void main(String[] args) {
// Creating the first object of the Person class
Person obj= new Person();
System.out.println("Name: " + obj.name);
System.out.println("Age: " + obj.age);
}
}Example of both class and object in Java
// Defining the Person class
class Person {
// Attributes (fields or variables)
String name;
int age;
// Method to display person's information
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Main class to create and use objects
public class Main {
public static void main(String[] args) {
// Creating an object of the Person class
Person person1 = new Person();
// Setting attributes for person1
person1.name = "Alice";
person1.age = 25;
// Calling the method to display person1's information
person1.displayInfo();
}
}
In this example, we define the class Person with some fields (name, age) and method (displayInfo). In the main method, we create an object named person1 of class Person and assign values to the name and age of the object person1. After assigning these values, we call the displayInfo() method on person1, which prints the name and age of person1. The output is shown below.
Name: Alice
Age: 25
How to assign values to Object fields?
We have two approaches for assigning values to object fields.
In first approach, We are using dot(.) operator, example below:
obj.name=”John”;
In the second approach, We use a method for assigning the value, example below:
// Defining the Person class
class Person {
// Attributes (fields or variables)
String name;
int age;
// Method to set person's data
void setData(String x, int y) {
name = x;
age = y;
}
// Method to display person's information
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Main class to create and use multiple objects
public class Main {
public static void main(String[] args) {
// Creating the first object of the Person class
Person person1 = new Person();
person1.setData("John", 26);
// Displaying person1's information
person1.displayInfo();
}
}
The code defines a Person class with two attributes: name and age. The setData method assigns values to these attributes. In the Main class, we create an Person object, set its data with "John" and 26, and print the information. The output is:
Name: John
Age: 26
can we create multiple objects of the same class? yes, absolutely. Let’s create multiple objects of person class.
Example of multiple objects of the same class
// Defining the Person class
class Person {
// Attributes (fields or variables)
String name;
int age;
// Method to display person's information
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Main class to create and use multiple objects
public class Main {
public static void main(String[] args) {
// Creating the first object of the Person class
Person person1 = new Person();
person1.name = "Alice";
person1.age = 25;
// Creating the second object of the Person class
Person person2 = new Person();
person2.name = "Bob";
person2.age = 30;
// Creating the third object of the Person class
Person person3 = new Person();
person3.name = "Charlie";
person3.age = 35;
// Displaying information for all three objects
person1.displayInfo();
person2.displayInfo();
person3.displayInfo();
}
}
In the main method, we create three separate objects of the Person class:
person1withnameset to “Alice” andageset to 25.person2withnameset to “Bob” andageset to 30.person3withnameset to “Charlie” andageset to 35.
We call displayInfo() for each object, which prints out the name and age of each individual Person object.
Name: Alice
Age: 25
Name: Bob
Age: 30
Name: Charlie
Age: 35
Conclusion
In Java, classes act as blueprints for creating objects, defining the attributes and behaviors that objects will have. By creating multiple objects from the same class, each instance can hold its own unique data, making classes and objects essential for building organized, reusable code in Java applications.