Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7182793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:50:44+00:00 2026-05-28T17:50:44+00:00

I am a beginner in Java . The first thing I learned was the

  • 0

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 .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:50:46+00:00Added an answer on May 28, 2026 at 5:50 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this is a beginner error... So I have a Java EE 6
hello i'm a beginner java developer and this is one question in a list
First things first, I am a Python beginner, with a typical C++/Java background for
I am a java beginner and for my first project I started building a
I'm a complete beginner trying to learn Java as my first language. When I'm
hi all this is my first question. I am beginner with web apps totally.
I am a beginner to Java and my first task is to parse some
This is not homework. I am a beginner (novice) java programmer, trying to read
I am a Java beginner and I would appreciate it if someone here can
I am a total total beginner in Java actually I am .net dev trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.