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
- Define class with fields
- Create constructor or set values directly
- Display method prints formatted string
Related OOP Basics exercises
- Practice Using Constructors in Java
- Practice Getters and Setters in Java
- Practice Static Members in Java
Practice all OOP Basics exercises · Run this idea in the Java compiler