• 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

access modifiers for main method

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is any other access modifiers are allowed to main method other than public and static? What are they? I am not sure of little bit.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No there arn't.
 
Anjana Ravindran
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is final or abstract not allowed? i saw somewhere either one of those is allowed. dont remember. want to confirm
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final is allowed for main() syntax is:

final public static void main(String a[])

if u inherit the class in which main is declared as final will get inherited without main() as it is final.

With Best Regards!
Sachin Dimble.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you change(not by swaping) the signature of main with any other valid modifiers it may be treated as a overloaded method for that class.. Not the entry point of execution !!
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the main method can have 'private' modifier too....the code runs fine....but for the sake of exam ( and because the JAva LAnguage Specification says), the main method must be 'public'

So mark 'error; in exam when u see a private main()

regards
niranjan
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It not possible to use any other keywords.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Anjana Ravindran]: Is any other access modifiers are allowed to main method other than public and static?

The only access modifier allowed (and required) for the main method is public. There are several other modifiers possible, but they aren't access modifiers.

Subsequent posts seem to ignore this issue, so I'll just pretend you did not use the word "access" in your post, and we'll move along...

[Anjana Ravindran]: is final or abstract not allowed? i saw somewhere either one of those is allowed. dont remember. want to confirm

A compiler could tell you this quite easily, couldn't it?

[Sachin Dimble]: if u inherit the class in which main is declared as final will get inherited without main() as it is final.

No, you would inherit the main() method from the superclass. (Which is pretty useless in this case, but it happens.) You would be prevented from declaring a new main() method in this class. Try it.

[Enge Chall]: If you change(not by swaping) the signature of main with any other valid modifiers it may be treated as a overloaded method for that class.

No, changing modifiers has nothing at all to do with overloading. Overloading would only occur because of changing the arguments - specifically, changing the types and/or order of the arguments.

[Niranjan Deshpande]: the main method can have 'private' modifier too....the code runs fine....

It may run fine on older JDKs, but I don't think it works on both modern JDKs. This is something they fixed in 1.4, if I remember correctly. Is there a specific JDK (1.4 or later) you've found that you can run a private main method on?

[Muruhanantham CC ]: It not possible to use any other keywords.

There is one which no one has mentioned yet. (Actually there's more than one, but the others are very obscure and not on the test.) I leave it as an excercise for readers to see if they can find it. Note that it is very easy to test this, so I encourage you to do so prior to posting any more guesses.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Niranjan Deshpande]: the main method can have 'private' modifier too....the code runs fine....

yes...jdk 1.5
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really? I just tested it again with JDK 1.5.0_05 on Windows 2000, using this code:

The result I get is "Main method not public."

Exactly which JDK 1.5 version are you using? Try typing java -version to see what it says.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,

synchronized and strictfp are allowed for a main() method. any other keywords?

Regards,
Vinodh Kumar. G
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Really? I just tested it again with JDK 1.5.0_05 on Windows 2000, using this code:

The result I get is "Main method not public."

Exactly which JDK 1.5 version are you using? Try typing java -version to see what it says.



Confirmed
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All, this is a very interesting question which sent me off onto a bit of reading/experimenting and here's what I found:
There is nothing special about a method called "main". It's just another method.
However, if you have a class with a main method and you wish to run the program and use this main as the starting point for code execution, you need to follow a few rules.
The JVM looks for a particular signature for the entry point of execution. The method must be called main, must be public, static and void, and accept a single argument, a String array (the array name is inconsequential).
Assuming that we're taking about the main method, here are a few comments.
The ONLY access modifier accepted is public. (This should answer Anjana's question).
It SHOULD be static.
Return type MUST be void.
It CAN be final (Anjana, why don't you just run a sample program?).
Sachin, if you extend a class with such a main, you won't inherit the main method because it is static not because it is final. A final method can be inherited (depending on visibility) but cannot be overridden. A static method is NOT inherited.
Using Java 1.4, I couldn't run a class with a private method. One thing I remember reading somewhere is that the main method should be public so that the JVM can have access to it. If it's private, you're restricting the JVM (and any other class) from accessing it.
Obviously, the main methods CANNOT be abstract as it cannot be run.
A method signature is defined by the name, argument list and the exception specification (the exceptions that the method declare that it might throw). So the JVM looks for main(String args[]). Any other signature and your program is toast. Runtime toast, mind you, not compile time toast.
It's the JVM that looks for main to run, not the compiler. The compiler will happily accept any legal variations for a method.
Repeat after me, the JVM runs the program, not the compiler.
Yes, synchronized and strictfp are valid. Synchronized might not make too much sense, though. What if you want to run two instances of this program under the same JVM? Somebody try it!
strictfp does make sense if you want to make sure that main adheres to the IEEE754 standard for floating point implementations. Very obscure, hence a possible question on the exam.

Sashi
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Conclusion: as the main is just another method all the regular rules that apply to the methods of a class will apply at compile time.
However, at runtime, the JVM looks for a method called main with the public access modifier, a void return type and the static modifier which accepts an array of String objects to run this program.
Did I miss anything?
Sashi
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Vinodh]: synchronized and strictfp are allowed for a main() method. any other keywords?

That's it. Synchronized is the one I was thinking of that should be known to everyone before they take the SCJP. (Though there's rarely any reason to use it on a main() method.) You can forget strictfp, for SCJP.

[Sasikanth]: Did I miss anything?

That was good. Two things I'd add which are technically already implied by "all the regular rules that apply to the methods of a class": (a) the argument can be final as well, and (b) under JDK 5, the array of strings can be replaced with varargs:


Oh, and from earlier:

[Sashikanth]: Sachin, if you extend a class with such a main, you won't inherit the main method because it is static not because it is final. A final method can be inherited (depending on visibility) but cannot be overridden. A static method is NOT inherited.

I would say it is inherited.

Calling "java Bar" will execute the main method from Foo. Not that this is useful for anything really, but it's true.
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, I don't believe you!
So I ran your snippet.
And you're right!
I stand corrected.
Sachin, my apologies.
I couldn't find enough detailed info about the status of inherited static members in the KB book (In Chap 5 or Chap 2) or the Java Complete Reference (What kind of complete reference is that?).
So I ran a few test progs and here's what I think:
static variables ARE inherited and retain their static nature in the child class as well. Inheritance follows the same rules as for non static members.
They can be overridden, but technically this is not overriding. We say that this is hidded (shadowed in C/C++ terminology).
If anyone can sumup the properties of static members that pertain to inhertance that'll be great!
Thanks,
Sashi
They cannot be hidden (overridden) by a non-static member.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dear
main methord is only final for the starting the program in java
 
Puran chandel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i can write public but i do misteken i write final.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic