Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Classes and Objects: A Practical Guide

Java Classes and Objects

Discover how Java’s class and object model turns real‑world concepts into reusable code.

Java is fundamentally object‑oriented. It breaks complex problems into manageable, self‑contained entities—objects—each with state and behavior.

An object can be anything that holds data (state) and offers actions (behavior). For instance, a bicycle has:

Before we dive into objects, let’s explore the blueprint that defines them: a class.


Java Class

A class is the template from which objects are instantiated. Think of it as a house plan that specifies floors, doors, and windows. Multiple houses can be built from the same plan, just as many objects can be created from a single class.


Creating a Class in Java

Define a class with the class keyword:

class ClassName {
  // fields
  // methods
}

Fields (variables) hold an object’s state, while methods encapsulate its behavior.

Example: a Bicycle class.

class Bicycle {
  // state
  private int gear = 5;

  // behavior
  public void braking() {
    System.out.println("Working of Braking");
  }
}

Here, Bicycle is a prototype. Multiple bicycle instances can be created, each sharing the same structure but holding independent data.

Note: private and public are access modifiers that control visibility. For more details, see Java access modifiers.


Java Objects

An object is an instance of a class. For example, Bicycle is the class, and MountainBicycle, SportsBicycle, TouringBicycle, etc., are distinct objects derived from that class.

Creating an Object in Java

Instantiate an object with the new keyword and the class’s constructor:

className object = new className();

// For Bicycle
Bicycle sportsBicycle = new Bicycle();
Bicycle touringBicycle = new Bicycle();

Constructors are special methods that share the class name. For further reading, explore Java Constructors.

Names like sportsBicycle and touringBicycle serve as references to their respective instances.

Multiple instances of the same class can coexist without interfering with each other.

Note: Class fields and methods are collectively known as members.


Accessing Members of a Class

Use the dot operator with an object reference to reach its members:

class Bicycle {
  int gear = 5;
  void braking() {
    // ...
  }
}

Bicycle sportsBicycle = new Bicycle();

sportsBicycle.gear;
sportsBicycle.braking();

Here, sportsBicycle accesses the gear field and invokes the braking() method.

In the next chapter, we’ll explore Java methods in depth.

Below is a complete example that ties these concepts together.


Example: Java Class and Objects

class Lamp {
  // Tracks whether the lamp is lit
  boolean isOn;

  void turnOn() {
    isOn = true;
    System.out.println("Light on? " + isOn);
  }

  void turnOff() {
    isOn = false;
    System.out.println("Light on? " + isOn);
  }
}

class Main {
  public static void main(String[] args) {
    Lamp led = new Lamp();
    Lamp halogen = new Lamp();

    led.turnOn();
    halogen.turnOff();
  }
}

Output:

Light on? true
Light on? false

In this program, Lamp defines an instance variable isOn and two methods that manipulate it. The Main class creates two lamp objects—led and halogen—and invokes their methods independently.

Each object holds its own copy of isOn, ensuring that operations on one lamp do not affect the other.


Example: Creating Objects Inside the Same Class

Objects can also be instantiated within the same class that defines them. Here’s how:

class Lamp {
  boolean isOn;

  void turnOn() {
    isOn = true;
    System.out.println("Light on? " + isOn);
  }

  public static void main(String[] args) {
    Lamp led = new Lamp();
    led.turnOn();
  }
}

Output

Light on? true

This pattern is useful for small demonstrations or utility classes where the main logic resides within the same class.


Java

  1. C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
  2. Java Abstract Classes and Methods: A Comprehensive Guide
  3. Mastering Java Nested and Inner Classes: Types, Examples, and Best Practices
  4. Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
  5. Mastering Java’s ObjectInputStream: A Comprehensive Guide
  6. Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
  7. Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
  8. Mastering Java Reader Class: Subclasses, Methods, and Practical Example
  9. Mastering Java Generics – Building Reusable, Type‑Safe Code
  10. Master Java: Understanding Objects, Classes, and Core OOP Concepts