Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

illegal combination of modifiers: abstract and static

 
del boy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I can't quite figure out why I get the following error:
illegal combination of modifiers: abstract and static
with the follwoing sample code:

or I get a similar erro when:
modifier static not allowed here
with the following code:

Why is like that ? Why can't I declare in my abstract or interface class a method to be implemented or extended as static by subclasses ?
This rule really baffles me.... Am I missing something ?
Thanks
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JLS, §8.4.3.1 abstract Methods:

It is a compile-time error for a static method to be declared abstract.

An abstract method is defined only so that it can be overridden in a subclass. However, static methods can not be overridden. Therefore, it is a compile-time error to have an abstract, static method.
Corey
 
del boy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey,
thanks for your reply.
An abstract method is defined only so that it can be overridden in a subclass. However, static methods can not be overridden. Therefore, it is a compile-time error to have an abstract, static method.

I am getting confused here... The following code compiles fine (and it definitely overrides the superclass meth) and prints the message defined in the meth of the sub-class ... It looks to me that subclass can override static superclass methods.
Many thanks in adbvance.
 
del boy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If static method cannot be ovverridden why I don't get an error message when I try to do so ?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not overriding the method, you're hiding it. Try this:

As you can see, dynamic binding is only used with the instance method, not the static method. That's because only the instance method is overridden. The static method is hidden. There is a distinct difference. Check out the JLS, §8.4.6 Inheritance, Overriding, and Hiding for more details.
Corey
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods can not be overridden only hidden. The difference is that hidden methods do not participate in polymorphism. Only overridden methods can participate in polymorphism.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic