Scope is the region of the program where a simple name refers to a particular declaration. Java has no “hoisting” of locals. You cannot read a local before it is assigned.
| Kind | Visible |
|---|---|
| Local / parameter | From declaration (parameters: whole method) to the end of the block |
| Instance field | All instance methods of the class (and nested types), subject to access |
| Static field | All code that can see the class, subject to access |
| Block | { int x; } — x is gone after the brace |
void demo(int n) {
if (n > 0) {
int t = n;
}
// t is not in scope
}
Shadowing: a local with the same name as a field hides the field. Use this.x for the field. Do not shadow casually — it is how constructor bugs ship.
Access is checked at compile time (and again by the JVM). It is not a security boundary against a determined attacker with the same process, but it is the encapsulation tool you actually use.
| Modifier | Same class | Same package | Subclass (other pkg) | World |
|---|---|---|---|---|
private | yes | no | no | no |
| (package / default) | yes | yes | no | no |
protected | yes | yes | yes* | no |
public | yes | yes | yes | yes |
*A subclass in another package may use protected members through its own type, not through an unrelated sibling instance. The exam wording is picky; production code rarely leans on that edge.
private unless you have a measured reason. No public mutable fields.public only if they are API. Package-private for collaborators in the same package.internal keyword. Package-private is the “friends in this folder” level.A static member belongs to the class, not to an instance. There is one copy of a static field per class (per class loader).
public class Counter {
private static int created;
private final int id;
public Counter() {
id = ++created;
}
public static int created() {
return created;
}
}
this or instance fields. They can be called as Counter.created().main is static so the launcher does not construct your class first.obj.created()) compiles and is misleading. Call it on the type.| On | Meaning |
|---|---|
| Local / parameter | Cannot rebind the name |
| Instance field | Must be assigned exactly once (declaration or every constructor / initializer) |
| Static field | Assigned once (declaration or static block) |
| Method | Cannot be overridden |
| Class | Cannot be subclassed |
final on a reference does not make the object immutable. final StringBuilder sb still appends. final int[] a still accepts a[0] = 1.
final int[] a = {1};
a[0] = 9; // legal
a = new int[1]; // compile error
static final primitives and Strings that are compile-time constants may be inlined into callers. Changing the value requires recompiling dependents.this is the current instance. It does not exist in a static method.this.x = x disambiguates a parameter from a field.this(…) as the first statement of a constructor delegates to another constructor of the same class.this escaping from a constructor (passing this to another thread or a listener) can publish a half-built object. Do not.public class Point {
private final int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point(int x) {
this(x, 0);
}
}
class Demo {
private static final int N;
private final int id;
static {
N = 10; // static initializer — once per class load
}
{
id = nextId(); // instance initializer — every construction
}
}
ExceptionInInitializerError. The class then stays unusable.final means deep immutability.this. in a constructor so you assign a parameter to itself: x = x;.protected “because subclasses might need it” — they usually need a method, not a field.final on fields you do not rebind. Immutable types when you share across threads.this.field = param. No this escape.~ Good luck with your revision! ~