Menu

Class and Object in Java
📰

Class and Object in Java

DEV Community: java·Esakki Muthu A·about 1 hour ago
#2VTLEQ9b
#devtojava#devto#dev#class#student
Reading 0:00
15s threshold

Java is an object-oriented programming language. Two important concepts in Java are Class and Object.

Let's understand them with a simple example.

What is a Class?

A class is a blueprint or template used to create objects.

It defines the properties (variables) and behaviors (methods) that its objects can have.

Example:

class Student {

String name;

int age;

void display() { System.out.println(name); System.out.println(age); }

Enter fullscreen mode

Exit fullscreen mode

}

Here:

Student is the class name.

name and age are variables.

display() is a method.

The class describes what information and behavior a student object can have.

What is an Object?

An object is an instance of a class.

We create an object using the new keyword in Java.

Example:

public class Main {

public static void main(String[] args) {

Student s1 = new Student();

s1.name = "Arun"; s1.age = 21;

s1.display(); }

Enter fullscreen mode

Exit fullscreen mode

}

Output:

Arun

21

Here:

Student is the class.

s1 is the…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.