DEV Community

Raj
Raj

Posted on

Java – Class and Object

INTRODUCTION

Java is an object-oriented programming language. It uses classes and objects to organize data and perform operations.

Class and Object are the fundamental building blocks of Object-Oriented Programming (OOP).

WHAT IS A CLASS IN JAVA?

A Class is a blueprint or template used to create objects. It defines the properties (variables) and behaviours (methods) that an object can have.

Explanation:

A class is a logical structure that groups related variables and methods together. It describes what data an object can store and what operations it can perform.

A class is a definition, not an individual object. We use a class as a common structure to create multiple objects.

Main Components of a Class:

  1. Variables – Used to store data.
  2. Methods – Used to perform specific operations.
  3. Constructors – Used to initialize objects.
  4. Blocks – Used to execute a group of statements.

Syntax:

`class ClassName {

// Variables

// Constructors

// Methods
Enter fullscreen mode Exit fullscreen mode

}
`
Example Program:

`class Student {

String name;
int age;

void study() {
    System.out.println("Student is studying");
}

void display() {
    System.out.println(name);
    System.out.println(age);
}
Enter fullscreen mode Exit fullscreen mode

}`

Explanation:

  • "class" is the keyword used to declare a class.
  • "Student" is the class name.
  • "name" is a variable used to store a name.
  • "age" is a variable used to store an age.
  • "study()" is a method that performs a specific action.
  • "display()" is a method used to display student information.

WHAT IS AN OBJECT IN JAVA?

An Object is an instance of a class. It is created using the class structure and contains its own data.

Explanation:

An object is an individual instance created from a class. It represents a specific entity in a Java program.

When an object is created, memory is allocated for its instance variables. Each object can store different values and use the methods defined in its class.

Multiple objects can be created from a single class.

Syntax:
`
ClassName objectName = new ClassName();

Example:

Student s1 = new Student();`

Explanation:

  • "Student" – Name of the class.
  • "s1" – Reference variable.
  • "new" – Keyword used to create an object.
  • "Student()" – Constructor call. ** HOW TO CREATE AND USE AN OBJECT?**

Objects are created using the "new" keyword. After creating an object, we can access its instance variables and instance methods using the dot (".") operator.

Example Program:

`class Student {

String name;
int age;

void display() {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {

public static void main(String[] args) {

    Student s1 = new Student();

    s1.name = "Raj";
    s1.age = 25;

    s1.display();
}
Enter fullscreen mode Exit fullscreen mode

}

Output:

Name: Raj
Age: 25`

Explanation:

  1. First, we create a class named "Student".
  2. We declare two instance variables: "name" and "age".
  3. We define a method named "display()".
  4. Inside the main method, we create an object named "s1".
  5. We assign values to the object's variables.
  6. Finally, we call the "display()" method using the object.

MULTIPLE OBJECTS IN JAVA

A single class can be used to create multiple objects. Each object has its own instance variables and can store different values.

Example Program:

`class Student {

String name;
int age;

void display() {

    System.out.println(name);
    System.out.println(age);

}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {

public static void main(String[] args) {

    Student s1 = new Student();

    s1.name = "Raj";
    s1.age = 25;

    Student s2 = new Student();

    s2.name = "Kumar";
    s2.age = 22;

    s1.display();
    s2.display();
}
Enter fullscreen mode Exit fullscreen mode

}

Output:

Raj
25
Kumar
22
`
Explanation:

we create two objects, "s1" and "s2", from the same Student class.
Both objects use the same class structure, but they maintain different values.
This allows us to manage multiple records using a single class.

**
TYPES OF VARIABLES IN JAVA**

Java has three main types of variables based on their declaration and usage.

  1. Instance Variable

An instance variable is declared inside a class but outside methods, constructors, and blocks.

Each object gets its own separate copy of instance variables.
`
class Student {

String name;
int age;
Enter fullscreen mode Exit fullscreen mode

}`

  1. Static Variable

A static variable is declared using the "static" keyword. It belongs to the class and is shared by all objects of that class.

`class Student {

static String college = "ABC College";
Enter fullscreen mode Exit fullscreen mode

}`

  1. Local Variable

A local variable is declared inside a method constructor or block. It can only be accessed within its declared scope.
`
class Student {

void display() {

    int marks = 90;

    System.out.println(marks);

}
Enter fullscreen mode Exit fullscreen mode

}`

WHAT IS A METHOD IN JAVA?

A Method is a block of code that performs a specific task. It executes when it is called.

Methods help divide a program into smaller and reusable sections.

Syntax:

`returnType methodName(parameters) {

// Statements
Enter fullscreen mode Exit fullscreen mode

}`

Types of Methods:

  1. Void Method

A void method performs an operation but does not return a value.

`void display() {

System.out.println("Hello Java");
Enter fullscreen mode Exit fullscreen mode

}`

  1. Return Type Method

A return type method sends a value back to the calling statement.

`int add() {

return 10 + 20;
Enter fullscreen mode Exit fullscreen mode

}`

  1. Parameterized Method

A parameterized method accepts values as input.

`void displayName(String name) {

System.out.println(name);
Enter fullscreen mode Exit fullscreen mode

}`

  1. Static Method

A static method belongs to the class and can be called using the class name without creating an object.

`class Test {

static void display() {

    System.out.println("Hello Java");

}
Enter fullscreen mode Exit fullscreen mode

}`

Method calling:

Test.display();

  1. Instance Method

An instance method belongs to an object and is generally called using an object reference.

`class Test {

void display() {

    System.out.println("Instance Method");

}
Enter fullscreen mode Exit fullscreen mode

}`

Method calling:

Test t1 = new Test();

t1.display();

WHAT IS A CONSTRUCTOR IN JAVA?

A Constructor is a special member of a class that is automatically called when an object is created.

It is mainly used to initialize the object's data.

Characteristics of a Constructor:

  1. A constructor has the same name as the class.
  2. A constructor does not have a return type.
  3. It is called during object creation.
  4. It can be overloaded.
  5. Java provides a default constructor if no constructor is explicitly declared.

Example Program:

`class Student {

String name;

Student() {

    name = "Raj";

}

void display() {

    System.out.println(name);

}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {

public static void main(String[] args) {

    Student s1 = new Student();

    s1.display();

}
Enter fullscreen mode Exit fullscreen mode

}

Output:

Raj`

Types of Constructors:

  1. Default Constructor
  2. No-Argument Constructor
  3. Parameterized Constructor

WHY DO WE USE CLASS AND OBJECT?

Uses of Class:

  1. To organize related variables and methods.
  2. To define a common structure for multiple objects.
  3. To improve code readability.
  4. To support code reusability.
  5. To make applications easier to maintain.

Uses of Object:

  1. To store individual data.
  2. To access instance variables.
  3. To call instance methods.
  4. To create multiple instances from one class.
  5. To represent real-world entities in a program.

Top comments (0)