DEV Community

Kavitha
Kavitha

Posted on

Objects and Classes in Java

OBJECTS
An object is a real-world entity that represents a state and behavior in a Java program. It is an instance of a class and occupies memory at runtime.

  • Stores data using variables (state)
  • Performs actions using methods (behavior)
  • Created using the new keyword Example:
Car c = new Car();

Enter fullscreen mode Exit fullscreen mode

Summary: An object is a runtime instance of a class that represents real-world entities.

CLASSES
A class is a blueprint or template used to create objects. It defines the properties and methods that objects will have.

  • Does not occupy memory until an object is created
  • Groups related data and behavior
  • Helps achieve object-oriented design

Example:

class Car {
    int speed;
    void drive() {
        System.out.println("Car is moving");
    }
}

Enter fullscreen mode Exit fullscreen mode

Summary: A class defines the structure and behavior of objects.

Top comments (0)