I have a clue of what serialVersionUID is for, and in as far as I don't I can look it up. But it can be a private variable that's not used inside the class. Is there some construct or so behind that? Are there other examples of private variables / methods that are not used inside the same class? Can I access private variables in Java without getters from a class that's not an inner class, like serialization does?
-
1you can use [reflection](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) – BeyelerStudios Jul 08 '15 at 12:29
-
downvoter please clarify. – Albert Hendriks Jul 08 '15 at 12:33
-
1No, please close. That's what I was looking for. – Albert Hendriks Jul 08 '15 at 12:37
3 Answers
You can access those values, and manipulate them via Reflection. By this mechanism you can check fields and invoke methods.
This field is being read by Java virtual machine which can do whatever it wants with your program including reading the private fields. Note that serialization-related methods like writeObject
can also be private, but this is not a problem for JVM either.
You can also access private fields/methods via reflection or java.lang.invoke API (however SecurityManager
may prevent you from doing this). In Oracle JDK/Open JDK there's also non-documented sun.misc.Unsafe
API which allows you to do many ughm unsafe things including even reading the raw data from objects.

- 97,161
- 19
- 222
- 334
Using java.lang.reflect.Member.setAccessible(true)
will override any privacy at runtime (not that there is much – the JVM will happily access private fields or methods given their name, see this answer.