A class is a blueprint; an object is a runtime instance created with new. If you know PHP or JavaScript classes, Java OOP is stricter: fields have types, access modifiers matter, and everything lives in a class (even main).
Defining and using a class
class User {
String name;
void greet() {
System.out.println("Hi, " + name);
}
}
User u = new User();
u.name = "Ada";
u.greet();
Non-public helper classes can live in the same file as Main; only one public top-level class per file.
Reference semantics
User a = new User(); User b = a; — both reference the same object; mutating through b affects a.
Important interview questions and answers
- Q: Class vs object?
A: Class is the template; object is a heap instance with its own field values. - Q: new keyword?
A: Allocates memory and calls the constructor; returns a reference.
Self-check
- What operator creates an object?
- How does Java OOP differ from JavaScript prototypes?
Tip: Compare with PHP OOP—Java fields need types; prefer private fields plus methods over public mutable state.