The best revenge is Massive Success……!

Full Stack Developer

Day 27 of Java Mastery: Defining Classes and Creating Objects in Java

Now that we know the basics of OOP (Object-Oriented Programming), let’s take the next step – learning how to create classes and objects in Java.

Think about a university. It has many students, professors, and courses. Instead of writing details separately for each student or professor, we create a common structure that defines what every student or professor should have. This structure is called a class, and the actual students and professors are objects created from it. Using classes and objects helps keep everything organized and easy to manage.

A class is like a design plan, and an object is the real thing made from that plan. Let’s break it down with simple examples.

What is a Class?

A class is a structure used to create objects. It defines what details an object will have (data) and what actions it can do (methods).

Think of a class like a blank student ID from. Once you have the form, you can fill in details like name and roll number to create many students IDs. Similarly, once we define a class, we can use it to create many objects with different values.

Standard Definition

A class in Java is a blueprint for creating objects. It defines properties (variables) and behaviors (methods) that objects of that class will have.

Example: University Students

At a university:

  • Every student has a name, roll number, and department.
  • Students can join courses, attend lectures, and check exam results.

Instead of writing details separately for each student, we create a Student class and use it to generate student records.

How to Define a Class in Java

Here’s how we write a Student class in Java:

class Student {
     String name;
     int rollNumber;
     String department;

     void joinCourse() {
          System.out.println(name + " joined a course");
     }
}
  • class Student -> Defines a Student class.
  • String name; -> Stores the student’s name.
  • void joinCourse() -> A method that allows a student to join a course.

What is an Object?

An object is a real-world example created from a class. If a class is a plan, then an object is the actual version made from that plan.

For example, if the Student class is a template, then every real student in the university is an object.

Standard Definition

An object is an instance of a class that contains real values stored in memory. It is created based on the blueprint (class) and represents something with properties (variables) and behaviors (methods).

How to Create an Object in Java

Here is how we create an object from a class:

public class Main {
     public static void main (String[] args) {
          Student s1 = new Student();
          s1.name = "Spyde";
          s1.rollNumber = 101;
          s1.department = "Computer Science";

          s1.joinCourse();
     }
}

Explanation:

  • Student s1 = new Student(); -> Creates an object named s1.
  • s1.name = “Spyde”; -> Assigns values to the object.
  • s1.joinCourse(); -> Calls the method to allow the student to join a course.

Output:

Spyde joined a course

Key Points to Remember

  • A class is a template that defines data and methods.
  • An object is a real instance of a class.
  • We use the new keyword to create an object from a class.
  • Objects store data and perform actions using methods.

Quick Summary

  • Classes help us create structured and reusable code.
  • Objects are real-world examples of a class.
  • We can store data in objects and perform actions using methods.
  • Classes and objects make programming organized and easy to manage.

Final Thoughts

Learning classes and objects is a key step in Java programming. Once you master this concept, you can build useful applications like student management systems, university databases, and attendance trackers.

Keep practicing, and soon, Java will feel simple and fun!


Discover more from Spyde's Blog

Subscribe to get the latest posts sent to your email.

Leave a comment

Discover more from Spyde's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Design a site like this with WordPress.com
Get started