简体   繁体   中英

JVM and private methods

I am a beginner in Java . The first thing I learned was the main() method of an executable class should be public and the reason given was since this method will be called by the JVM it should be visible outside the class and hence should be public. Now while studying serialization I find that the writeObject() and readObject() private methods of a Serializable class can be called by the JVM while serializing and de-serializing an object ! If they are private methods then how can JVM call them ? If it can then why it can't call the main() method ?

After flipping through some java documentation , I read this line " JVM can access private methods of an object " . Since we call readObject() using an instance of ObjectInputStream so it is accessible to JVM , whereas main() method being a static or class method and called without instantiating any object of the class should be public in order to be accessible to JVM ! Does that make sense ? I don't know .

It is all about convention. The JVM can call every method everywhere - it is the JVM, the Boss. If the JVM can't call it, nobody can. Imagine the JVM is the place were your program gets executed, it is some kind of a proxy between you and your bytecode.

writeObject might not be of use outside your class. It is an internal only method, just for you. Thus the makers of the JVM agreed to leave it private. If it is not private, other classes in your program are able to call it.

The main() Method is a method which might be called by other Java processes. If it is private, other programs are unable to start your program. It has not much to do with the JVM, except you could say that normally the JVM allows you (!) to only call public (package, protected) methods of your code.

Well in general I agree with @Christian, but I have 2 more notes:

  1. The main() function could be also made private.

  2. You can also call private method/function, if you really need.

1 JVM could call main() anyway. Making it public enables another code of your application to call main() method also. Normally, nobody does it, however. The main reason, why main() is public, is historic one, AFAIC. Other program languages also have main() function and there it is public.

2 If you really need, you can use Core Reflection API in order to call private method or to access private field. It looks something like this:

      import java.lang.reflect.Field;
      class SimpleKeyPair {
         private String privateKey = "Cafe Babe"; // private field
      }
      public class PrivateMemberAccessTest {
        public static void main(String[] args) throws Exception {
          SimpleKeyPair keyPair = new SimpleKeyPair();
          Class c = keyPair.getClass();
          // get the reflected object 
          Field field = c.getDeclaredField("privateKey");
          // set accessible true 
          field.setAccessible(true);
          // prints "Cafe Babe"
          System.out.println("Value of privateKey: " + field.get(keyPair)); 
          // modify the member varaible
          field.set(keyPair, "Java Duke");
          // prints "Java Duke"
          System.out.println("Value of privateKey: " + field.get(keyPair)); 
        }
      }

http://www.jguru.com/faq/view.jsp?EID=321191

See also this great answer https://stackoverflow.com/a/2489644/1137529

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM