3 ms·
First note: I have never dealt with the internal workings of the Java VM, so this is just speculation. I also haven't tested any of this, but its still fun to
by afhof 16y ago
First note: I have never dealt with the internal workings of the Java VM, so this is just speculation. I also haven't tested any of this, but its still fun to speculate.
In java private variables and functions are not accessible from outside the class. This means that the compiler would be able to make some assumptions about the nature of these members in the effect of optimization. When calling a public function of another class in java, I suspect that the name of the function is mapped to the actual bytecode at invocation time after being looked up in some sort of trie/tree/hashtable. So, for every function call or varaible access, there would have to be a lookup. On the other hand, if the members were declared private, the compiler could directly link a caller to the function and skip the lookup. If this is the case, then setting all these projects' sources to use only public would mean a substantial performance loss.
I would love to be corrected if this is not the case, as I haven't taken a course in OO compilers yet.
- SoftwareMaven 16y agoAll Java method calls use the virtual function table, if for no other reason than to be able to throw a "MethodNotImplementedException" (or whatever the actual exception is, can't remember now). You can also inject code into the call stack using aspect programming.
- ewjordan 16y agoIn java private variables and functions are not accessible from outside the class. Not quite - using reflection you can dig into private fields and do your worst to them, you just need to tag each private member's Field object with with field.setAccessible(true) before accessing it reflectively. The only hitch is that you might get a SecurityException, but you can avoid that if you're running your code on your own JVM (by default, it should work just fine, it's if you're deploying applets or something like that where you might get the exception due to the different sandboxing rules).