Java Object Creation and Usage: Classes, Instantiation, Member Access, Getting Started from Scratch

What are Classes and Objects?

Before learning about object creation and usage in Java, let’s first understand two core concepts: classes and objects.

Class: The “Template” for Objects

Imagine you’re designing a mobile phone. Before production, you’d draw a “phone blueprint” outlining its attributes (color, screen size, number of cameras) and functions (making calls, taking photos). In Java, this blueprint is called a class—it serves as the template for objects, defining what attributes and methods an object should have.

Syntax for a Class:

class ClassName {
    // Member Variables (Attributes): Describe object characteristics
    dataType attribute1;
    dataType attribute2;

    // Member Methods (Behaviors): Describe object actions
    void methodName() {
        // Method body: Implementation of the action
    }
}

Object: An “Instance” of a Class

With the blueprint (class), you can produce a specific phone (object). In Java, an object is a concrete instance created from a class. It has the attributes and methods defined by the class and can independently store data and execute actions.

Analogy: A class is a “phone blueprint,” and an object is a “specific phone” (e.g., “my phone,” “your phone”).

How to Define a Class and Create Objects?

Let’s use a simple “Student” class to demonstrate class definition and object creation step by step.

Step 1: Define a “Student” Class

Suppose we define a Student class with attributes (name, age) and a method (self-introduction):

// Define the Student class (object template)
class Student {
    // Member variables: Describe student attributes (name, age)
    String name; // Name
    int age;     // Age

    // Constructor: Initializes the object (automatically called when creating an object)
    // Constructor name must match the class name and has no return type
    Student(String studentName, int studentAge) {
        // this.attribute = parameter: Assign parameters to object attributes
        this.name = studentName;
        this.age = studentAge;
    }

    // Member method: Describes student behavior (self-introduction)
    void introduce() {
        System.out.println("Hello everyone! My name is " + name + ", and I'm " + age + " years old.");
    }
}

Step 2: Create a “Student” Object (Instantiation)

After defining the class, use the new keyword to create an object from the class (instantiation). The syntax is:

ClassName objectName = new ClassName(parameterList);

For example, create a student object named “Xiaoming” who is 18 years old:

// Create a Student object (instantiation)
Student xiaoming = new Student("Xiaoming", 18);

Step 3: Access Object Members

After creating the object, access its attributes and methods using the objectName.member syntax:
- Access attributes: objectName.attributeName
- Call methods: objectName.methodName()

Example:

// Access object attributes
System.out.println("Xiaoming's name: " + xiaoming.name); // Output: Xiaoming's name: Xiaoming
System.out.println("Xiaoming's age: " + xiaoming.age);   // Output: Xiaoming's age: 18

// Call object method
xiaoming.introduce(); // Output: Hello everyone! My name is Xiaoming, and I'm 18 years old.

Creating and Independence of Multiple Objects

A single class can create multiple objects, with each object having independent attributes. For example, create another student object “Xiaohong”:

// Create a second Student object
Student xiaohong = new Student("Xiaohong", 19);

// Access Xiaohong's attributes and methods
System.out.println("Xiaohong's name: " + xiaohong.name); // Output: Xiaohong's name: Xiaohong
xiaohong.introduce(); // Output: Hello everyone! My name is Xiaohong, and I'm 19 years old.

Notes

  1. Constructor:
    - No return type (not even void), and must exactly match the class name.
    - If no constructor is manually defined, Java provides a default parameterless constructor (ClassName()).

  2. Default Values for Member Variables:
    - If uninitialized, Java assigns default values (e.g., int defaults to 0, String defaults to null).
    - Example: If no age is passed, age will default to 0.

  3. Member Access:
    - Members must be accessed via the object name (cannot access instance members directly via the class name).

Summary

  • Class: A template for objects, defining attributes and methods.
  • Object: An instance of a class, created using the new keyword to store data and execute methods.
  • Member Access: Use objectName.attribute or objectName.method() to operate on members.

Now, try defining your own class (e.g., an “Animal” class with attributes like “sound” and a method “makeSound”), then create multiple objects to experience the relationship between classes and objects!

Xiaoye