This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marker interface

 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I did not understand use of marker interfaces and following statement. I read like
'marker interfaces just tell the compiler that the objects of the classes
implementing the interfaces with no defined methods need to be treated differently.(ex cloneable, serializable)

'Any ideas, suggestions, resources, links, sample code highly appreciated. Thanks in advance
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gudim gudi wrote:
Any ideas, suggestions, resources, links, sample code highly appreciated.


A marker interface is an empty interface (has no methods). It's used as a tag to identify objects at runtime using for example the instanceof keyword, like


Note that using marker interfaces is a quite questionable practice and you should avoid introducing your own.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A marker interface is a (mostly obsolete) way of providing meta-data about instances of a particular type.
For instance, having a class implement java.io.Serializable is a way to tell the JVM "Hey, my instances are eligable for object serialization, so if they get passed to an ObjectOutputStream, you can work your serialization magic and not throw a NotSerializableException, mkay?".
Nowadays, annotations would be the preferred way to specify meta-data.
 
Ulf Dittmer
Rancher
Posts: 42968
73
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic