• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Main's modifiers

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is specified that the correct signature of main method is -
public static void main(String args[]){/*...*/}
Static because the java interpreter calls this method in a static context, and public because it should be accessible to the interpreter.
But if this thing is in the Java language specification... how come I'm able to compile (and run - perfectly) an application using this -
<code>
private static void main(String args[]){/*...*/}
</code>

this -
<code>
protected static void main(String args[]){/*...*/}
</code>

and this -
<code>
static void main(String args[]){/*...*/}
</code>
??
And after you've solved this problem of mine... will you please explain why would this -
//MeraMain.java
public class MeraMain{
private static void main(String args[]){
System.out.println("MeraMain's main!!");
}
}
class MeraSubMain extends MeraMain{}
when invoked with the following command -
java MeraSubMain
would give the following output -
MeraMain's main!!
??
As the MaraMain Main is, after all, private.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just because a particular JVM lets us do something not in the specification doesnt mean we should do it.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I think we can safely say - you better answer the question according to the specification rather than on what some JVMs allow on the SCJP!
Bill
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,
For the purpose of the exam the correct declaration is:

You'll find a number of discussions on the topic both here at JavaRanch and on Sun's bug site. Many people think this is a bug in the compiler or that the specification should be changed.
I think that JDK 1.3 and 1.2 only check for the signature of the method, which does not take the access modifier into account.
Essentially, main() is treated as any other method except for the fact that Java looks for it when you start an application. Inheritance rules still apply; which is why you're seeing the behaviour given in your example.
When you enter <code>java MeraSubMain</code>, Java searches the class for main(), when it doesn't find it, it searches the superclass MeraMain and executes its main() method (totally ignoring the access modiifer 'private').

Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<h6>Are static methods inherited? </h6>

I don't know ?
see this code.


maha anna and others , please clarify this one and the one gaurav asked



if you dont create main method in your class java calls the main method of the parent class, even if both the classes are in different files!!

------------------
KS
 
Gaurav Nagar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply guys... I'm gonna assume for now that (correct) main cannot be private. But that is from the exam point of view (I'm gonna give it soon).
But the fact is... I can compile and run the code specified!! And I've tried this on many different nodes with different OS (having different JVM's of course).
Thanks... but I'm doomed if this Q comes in the test
Thanks
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it comes up on the test I'll be surprised. I think all that may come up is some kind of question that tests whether or not you know the correct signature which is of course
 
Gaurav Nagar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Passed SCJP2 with 93% !! And guess what... hmmmmmmm... I dont think I'm allowed to discuss the questions but I can assure you that you don't have to worry about the compiler bug. Just rely on the JLS.
It's the only bible of Java.
And thanks for your replies.
On to developer test now... will someone sponser me .
On a more serious note... as you all must know by now that there was an earthquake in Gujarat (India)... The scene there is really bad guys... I request each and everyone of you to pray for those who have been affected.
And make a contribution if you can... anything would help.
-gaurav
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does JLS mean ? Please answer. Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic