1

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?

Albert Hendriks
  • 1,979
  • 3
  • 25
  • 45

3 Answers3

2

You can access those values, and manipulate them via Reflection. By this mechanism you can check fields and invoke methods.

Community
  • 1
  • 1
T.G
  • 1,913
  • 1
  • 16
  • 29
1

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.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
1

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.

Community
  • 1
  • 1
llogiq
  • 13,815
  • 8
  • 40
  • 72