Java Classes and Objects: From Definition to Instantiation, the Basics of Object-Oriented Programming

What is Object-Oriented Programming (OOP)?

Imagine you’re designing a mobile game with a “Player” character. Each player has attributes like name, level, and health, and can perform actions like attack, move, and level up. In Java, the “Player” is a class, while a specific player (e.g., “Xiaoming”) is an object of that class. Object-Oriented Programming (OOP) centers on abstracting real-world entities into “classes” and simulating/operating on these entities through “objects.”

Class: The “Blueprint” for Objects

A class is a template for objects, defining their attributes (data) and behaviors (methods). For example, a Person class describing human characteristics:

class Person {
    // Member variables (attributes): Describe the object's state
    String name;  // Name
    int age;      // Age

    // Member methods (behaviors): Describe the object's functionality
    void sayHello() {
        System.out.println("Hello everyone, my name is " + name + " and I'm " + age + " years old.");
    }
}
  • Member Variables: name and age describe basic human information (state).
  • Member Methods: sayHello() describes human behavior (functionality).

Instantiating Objects: From “Blueprint” to “Real Object”

A class is just a blueprint; to use it, you create an object (instantiation). The syntax is: Class Name Object Name = new Class Name();

// Create a Person object
Person person = new Person();
  • new Person(): Creates a new object using the class template with the new keyword.
  • person: A variable pointing to the object (similar to a “pointer,” used to access attributes/methods later).

Accessing Object Members

After creating an object, use the . operator to access its members:

1. Assigning Values to Member Variables

person.name = "Xiaoming";  // Set name attribute
person.age = 20;           // Set age attribute

2. Calling Member Methods

person.sayHello();  // Output: Hello everyone, my name is Xiaoming and I'm 20 years old.

Constructor: Initializing Objects During Creation

To set attributes directly when creating an object, use a constructor (same name as the class, no return value):

class Person {
    String name;
    int age;

    // Default constructor (Java provides this by default; it disappears if a custom constructor is defined)
    public Person() {
        name = "Default Name";  // Initialize default values
        age = 0;
    }

    // Parameterized constructor: Directly assign values when creating an object
    public Person(String name, int age) {
        this.name = name;  // "this" refers to the current object, avoiding name conflicts with parameters
        this.age = age;
    }

    void sayHello() {
        System.out.println("Hello everyone, my name is " + name + " and I'm " + age + " years old.");
    }
}

Using Constructors to Create Objects:

// Use default constructor
Person p1 = new Person();
p1.sayHello();  // Output: Hello everyone, my name is Default Name and I'm 0 years old.

// Use parameterized constructor
Person p2 = new Person("Xiaohong", 18);
p2.sayHello();  // Output: Hello everyone, my name is Xiaohong and I'm 18 years old.

Full Example: From Class to Object in Practice

class Person {
    String name;
    int age;

    // Default constructor
    public Person() {}

    // Parameterized constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    void sayHello() {
        System.out.println("Hello everyone, my name is " + name + " and I'm " + age + " years old.");
    }
}

// Main class (program entry point)
public class Main {
    public static void main(String[] args) {
        // Create objects
        Person p1 = new Person("Xiaoming", 20);
        Person p2 = new Person();

        // Call methods
        p1.sayHello();  // Output: Hello everyone, my name is Xiaoming and I'm 20 years old.
        p2.name = "Xiaogang";
        p2.age = 22;
        p2.sayHello();  // Output: Hello everyone, my name is Xiaogang and I'm 22 years old.
    }
}

Key Notes

  1. Naming Conventions: Class names start with a capital letter (e.g., Person), while member variables and methods start with a lowercase letter (e.g., name, sayHello).
  2. Default Values: Unassigned member variables have default values (e.g., int defaults to 0, String defaults to null).
  3. Object Independence: Each object’s member variables are independent (e.g., modifying p1.name doesn’t affect p2.name).
  4. Encapsulation Recommendation: In practice, member variables are often private (to hide internal details), accessed via getter/setter methods (e.g., getName() to retrieve the name).

Summary

A class is a “blueprint,” and an object is a “real item.” By instantiating objects with new, accessing members with ., and initializing attributes via constructors, you can perform basic Java OOP operations. Mastering classes and objects will ease learning subsequent concepts like encapsulation, inheritance, and polymorphism!

Xiaoye