Create a Simple Class in Java: Explanation & Practice

Define a class with attributes and methods

Problem summary

Create a Person class with name and age, and a method to display them.

Starter code

// Create Person class with:
// - String name
// - int age
// - void display() method

public class Main {
    public static void main(String[] args) {
        // Create person and call display()
        // Output: Name: Alice, Age: 25
    }
}

Expected output and test cases

  • Display person info
    Name: Alice, Age: 25

Hints

  1. Define class with fields
  2. Create constructor or set values directly
  3. Display method prints formatted string

Related OOP Basics exercises

Practice all OOP Basics exercises · Run this idea in the Java compiler