• Post Reply Bookmark Topic Watch Topic
  • New Topic

small doubt regarding Marker Interface  RSS feed

 
Gaurav Lala
Greenhorn
Posts: 12
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a marker interface have constants ?

We know that marker interfaces provided by JVM such as Cloneable, Serializable, etc. do not contain anything. But as the definition says, "Marker Interface are those which do not contain any methods", and we can define an interface with only constants (public, static and final). So will that interface be considered as marker or a normal one?
 
Tim Moores
Saloon Keeper
Posts: 4032
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a marker interface, as it's not "marking" anything. Using interfaces for constants was sometimes done by various and sundry developers, but has long since been judged to be an anti-pattern.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even marker interfaces themselves are a thing of the past. New code should define annotations, not marker interfaces.
 
Gaurav Lala
Greenhorn
Posts: 12
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim, what do you mean by, "it is not marking anything ?"
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Even marker interfaces themselves are a thing of the past. New code should define annotations, not marker interfaces.


Joshua Bloch: Effective Java 2nd Edition, p 179

A marker interface is an interface that contains no method declarations, but merely designates (or “marks”) a class that implements the interface as having some property. For example, consider the Serializable interface (Chapter 11). By implementing this interface, a class indicates that its instances can be written to an ObjectOutputStream (or “serialized”).

You may hear it said that marker annotations (Item 35) make marker interfaces obsolete. This assertion is incorrect. Marker interfaces have two advantages over marker annotations. First and foremost, marker interfaces define a type that is implemented by instances of the marked class; marker annotations do not. The existence of this type allows you to catch errors at compile time that you couldn’t catch until runtime if you used a marker annotation.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew someone was going to pop up with that one. Yeah, there may be a few places where a marker interface is still appropriate, but 99% of their previous uses can be better represented with an annotation. My view; take it or leave it.
 
Gaurav Lala
Greenhorn
Posts: 12
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys, each & everyone's view really helped in clearing my doubt...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!