I'm new to java. I have been reading about Getter/Setter (Why to use getter/setter and getter/setter poor design). While I read on Oracle Understanding JIT. I've two questions as follows :
My question is that, if JIT replaces some getters/setters with actual value while compiling a code, how is the purpose of getter/setter being fulfilled?
How will we achieve encapsulation ?
if JIT replaces some getters/setters with actual value while compiling a code how purpose of getter/setter is fulfilled in java particular ?
Because it's only an optimization. The getters and setters still work exactly as expected - and if you later change the implementation to use different fields or some other mechanism, the JIT compiler will have to change how it transforms the code as well.
For the most part, your reasoning about program design shouldn't care about the JIT compiler at all - that's an implementation detail about how your program happens to get executed. You may well want to care about it occasionally to achieve better performance, but it doesn't change whether or not your program design still holds up.
Just because a JIT may compile code that uses a getter to effectively just get the field directly doesn't mean it's appropriate to expose the field publicly yourself - that would expose an implementation detail, making it hard to change later. It doesn't matter if you change things which make your JIT-compiled code invalid later, because by the time you're running the new code, the JIT compiler won't be aware of what it did with the old code anyway.
See more on this question at Stackoverflow