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 make them while still doing something else to make it useful? How can I access private variables myself from another class without creating getters?
Asked
Active
Viewed 61 times
0

Mark Rotteveel
- 100,966
- 191
- 140
- 197

Albert Hendriks
- 1,979
- 3
- 25
- 45
-
think of writeObject() and readObject(), also used for serialization (ref [Uses of readObject/writeObject in Serialization](https://stackoverflow.com/questions/5911833). – f_puras Jul 08 '15 at 11:57
-
That's for [serialization/deserialization](http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html) *The serialization runtime associates with each serializable class a version number, called a serialVersionUID ... A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long* – A4L Jul 08 '15 at 11:59
-
3Did you guys even read the question? – Albert Hendriks Jul 08 '15 at 12:03
-
Did you read the accepted answer? "*It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class*" – Jul 08 '15 at 12:03
-
@a_horse_with_no_name ok, but my question is: how is that possible? – Albert Hendriks Jul 08 '15 at 12:05
-
1The javadocs states: *that must be static, final, and of type long* no word about `private`, so you may declare it the way you want and since it is final and static this makes sure it is not changed for the class. The javadoc also states that it is **used** by the serialisation runtime (the construct behind that). – A4L Jul 08 '15 at 12:06
-
2I want to know more about how the serialization runtime can access private variables. And I want to learn how I can access private variables myself, from another class. – Albert Hendriks Jul 08 '15 at 12:11
-
Then you should ask a different question. – Jul 08 '15 at 12:16
-
Dude, it starts with explaining that I know what serialVersionUID is for. The question is pretty clear and private is in capitals. Asking the same question twice would be crosspsoting. – Albert Hendriks Jul 08 '15 at 12:19
-
downvoter please clarify after all this discussion – Albert Hendriks Jul 08 '15 at 12:24
-
Have a look at ReflectionUtils. http://docs.spring.io/spring-framework/docs/2.5.x/api/org/springframework/test/util/ReflectionTestUtils.html. But in general, variables are private to achieve the benefits of encapsulation, and you're throwing that away. – Don Branson Jul 08 '15 at 12:26
-
1The question should have avoided asking about serialVersionUID to avoid being a duplicate. You should have centered on the "how do I read a private", which is also answered elsewhere; also, the Java runtime is free to do things in a different way. Writing "totally not a duplicate" does not magically make it so. – tucuxi Jul 08 '15 at 12:26
-
I'm not asking about serialVersionUID. It's the only example I know. Also, there's probably an often duplicated question about serialVersionUID which is also the only question about serialVersionUID ever asked, which is the probable cause for the prejudice. – Albert Hendriks Jul 08 '15 at 12:31
-
2First the runtime is the runtime, it can do a lot of stuff, for example when a class containing constants is loaded where do you think the values come from which you can use reguardles of access modifier inside or outside the class itself? The runtime writes those values for you and thus can read them too. Second, you as language user can also do a lot of stuff using reflection (assuming all installed security mamagers permit it), take a look at [setAccessible](http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/AccessibleObject.html#setAccessible%28boolean%29), for example... – A4L Jul 08 '15 at 12:33
-
Many thanks for the people who upvoted me, but it still seems to be a duplicate. About how serialization accesses private variables, see also here: http://stackoverflow.com/questions/4245301/serializing-private-variables-in-java – Albert Hendriks Jul 08 '15 at 12:44
-
Here is an interesting [question](http://stackoverflow.com/q/2481862/1113392) and [answer](http://stackoverflow.com/a/2489644/1113392) for you to read – A4L Jul 08 '15 at 12:56