Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method private

 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was solving a 1.4 mock exam. It had private main method and print statement in it. The answer provided is that the print statement will be executed.
I tried this on my machine but found that I was getting a runtime message that main method is not public.

I am totally confused

Please help me
 
bing marquez
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi joshua,

There is only one signature of main that the JVM looks for to launch
an application, i.e. void main(String[] args) and that is must be
public and static. However you,re free to declare another method named
main in your code, apart from the main() method required by the JVM, but
with a different signature.

consider:



Anyway, can you show the code...
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joshua,

perhaps you should show us the code. Was it perhaps something like this?




Yours,
Bu.
 
Deepak Mahboobani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The Java Virtual Machine Specification," which in section 2.17.1 states, "The method main must be declared public, static, and void."

Sun introduced this problem in version 1.2 of Java. As of 1.3 the problem is still there.

After a quick search for "private main" at java.sun.com, I found (requires login):

http://developer.java.sun.com/developer/bugParade/bugs/4252539.html

As you can see, the main issue is a known problem with a bug report opened against it. However, Sun has closed the bug report and the problem will NOT be fixed, "The runtime allows call to private methods, because of reflection. Fixing it will cause potential troubles."

So there you have it.

However, be careful while taking the certification exam. Last time that I checked, the proper answer to "How do you define a main?" remains: "The method main must be declared public, static, and void." Reality and the correct answer seem to disagree.
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As i have read main method should be public only from 1.4 or above before 1.4 it can have any access modifier.Please correct me if iam wrong.

Regards,
Praveen
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks for your replies.

Here is the code

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic