Classes & Objects
Every program since java1-1 has lived inside a class out of necessity. This chapter explains why — fields, constructors, methods, and the access-control model that governs who can touch what.
Fields & Constructors
A constructor shares the class's exact name, has no return type, and runs once when new Account(...) is called. this refers to the current instance — needed here specifically because the parameter names shadow the field names.
Java's Four Access Levels
Where C++ has three access levels (public, protected, private), Java has a genuine fourth: package-private, the default when no modifier is written at all. A member with no modifier is visible to any class in the same package, but nowhere else — a level of granularity C++ has no direct equivalent for.
| Modifier | Visible from | C++ equivalent |
|---|---|---|
| public | anywhere | public |
| protected | same package + subclasses anywhere | protected (subclasses only, no package concept) |
| (none) — package-private | same package only | no direct equivalent |
| private | same class only | private |
Why a Fourth Level Exists
C++ has no built-in concept of a "package" the way Java does, so it has nothing to attach a package-scoped access level to in the first place. Java's package system gives classes in the same package a middle ground: more open than private, but without exposing a member to every caller everywhere the way public does. It's a genuinely different granularity, not just a naming difference.
Instance Methods vs. Static Methods
main, back in java1-1, was already static — this is why: a static method belongs to the class itself, not to any particular instance, which is exactly why the JVM can call main without ever constructing an object first.
private and every method as narrowly scoped as the design allows — widen to package-private, then protected, then public only when a real caller outside that scope genuinely needs access.
Coding Challenges
Write a class Rectangle with private width and height fields, a constructor that sets both using this, and a public method area() that returns their product.
📄 View solutionWrite a class with one package-private field (no modifier) and explain in a comment exactly which callers can and cannot access it, contrasting it with what private and public would each allow.
📄 View solutionWrite a static factory method inside a class called origin() that returns a new instance representing coordinates (0, 0), and explain why it must be static in order to be called before any instance exists.
📄 View solutionChapter 4 Quick Reference
- A constructor shares the class name exactly, has no return type, and runs once per new instance
- this disambiguates a field from a same-named parameter inside a constructor or method
- Java has four access levels: public, protected, package-private (the default, no modifier), and private — a genuine fourth level C++'s three don't have
- Package-private exists because Java has a real package concept C++ doesn't — a middle ground between private and public
- static methods belong to the class itself, not an instance — exactly why main can run with no object constructed first
- Next chapter: inheritance, polymorphism, and Java's single-inheritance-of-classes model