Java
Objects & classes
A practical reference with examples you can run.
Introduction
Objects group data with related behavior. A class describes how to create those objects. Model a small concept first, such as a learner with a name and a greeting, and keep responsibilities clear.
Syntax & example
public class Main {
public static void main(String[] args) {
Learner learner = new Learner("Alex");
System.out.println(learner.greet());
}
static class Learner {
String name;
Learner(String name) { this.name = name; }
String greet() { return "Hello, " + name; }
}
}Expected output
Hello, Alex
Common mistakes
- Using a variable before assigning a value to it.
- Mixing text and numbers without converting deliberately.
- Skipping an empty input or a boundary value when testing.
Practice
Create a second learner with a different name.
Main.javaYour ideas, in code.
Loading your editor…
Run your code to see the output here.
Ready to buildUTF-811 lines