You're confusing the two different "compile times" (which is easily done).
The usual "compile time" we refer to is literally that: When the source code text is converted into bytecode. But the JIT (the Just-In-Time compiler) happens at runtime.
The encapsulation happens because any code using your class only sees the getters and setters. The JIT can then inline those getters and setters, but that's a runtime optimization, not something code can use.
Consider the common pattern where the setter is private and the getter is public:
class Square {
int size;
public Square(int size) {
this.setSize(size);
}
private void setSize(size) {
this.size = size;
}
public int getSize() {
return this.size;
}
}
While the JIT (or even the main compiler) may well inline setSize
, and the JIT may well inline getSize
, code using Square
instances cannot use that in any way to modify the size
field.