• 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 method and abstract keyword

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

While playing around with main method in my Java code, I got this error message :
"Illegal modifier for the method main; only public, protected, private, abstract, static, final, synchronized, native & strictfp are permitted."

Upon reading the complete list, I found abstract keyword is permitted but I can't get the reason why this keyword is allowed as main is a static method. Can anyone please throw some light on it.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us the code, so we have something more concrete to discuss about.
 
Neha Agnihotri
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.
You can add any illegal keyword in the signature of main method, say,
public static volatile void main(String args[])
or
public static default void main(String args[])

and get the same compilation error which is :
"Illegal modifier for the method main; only public, protected, private, abstract, static, final, synchronized, native & strictfp are permitted."


I'm interested in knowing how/why abstract keyword be used for main method. Please help!
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not specific to the main method.
That error simply lists all the allowed method  modifiers that could be there, whether they would be valid in every case or not.
volatile and default are never valid for a method.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is an automated error message listing all the modifiers that one might use before a message name. What modifier did you use to get that error message? I don't think you should interpret that message as meaning you are allowed every combination of those modifiers.
 
Neha Agnihotri
Ranch Hand
Posts: 39
  • 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 got confused because the first line of the error message clearly mentions the method name as main and then lists the legal list of modifiers.

BTW I have to constantly log in every few minutes  to post a reply
 
Neha Agnihotri
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think that is an automated error message listing all the modifiers that one might use before a message name. What modifier did you use to get that error message? I don't think you should interpret that message as meaning you are allowed every combination of those modifiers.



I deliberately added volatile keyword in the main method's signature and got that error message. While I understand volatile/default don't make sense here, I got confused regarding abstract keyword.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That goes to show you how difficult it is to write good compiler error messages that don't risk causing confusion to their readers.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's method like any other, you can make it abstract, but you won't be able to run that class, you'd have to implement it in subclass
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't make a method both abstract and static; it is also not possible to change methods from instance type to static or vice versa in subclasses.
 
Ivana Kilibarda
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was going to edit my post

What I wanted to say is main method is a method just like any other and you can have any legal modifier, you can overload it, you can override it/or hide it, make it static or make it instance, have it to return a value or have any type od parameter, but it won't run your class, JVM needs exact public static void main(String... args) declaration
 
reply
    Bookmark Topic Watch Topic
  • New Topic