Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is better (Accessing constants defined in interface)

 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a interface which only has constants declared in it. Now to access these constants from a class I can have two ways
1) As the constants are public and static I can access them directly in my class as InterfaceName.ConstantVar
2) I can make the class implememt the interface and then I can directly access the constants of the interface in my class as if they were declared in the class itself. ConstantVar
Which is better?
Thanks & Regards,
Deepak
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no point in implementing such an interface. The good way to access static fields is to use the ClassName.FieldName format so that some one looking at your code will know that this field is static.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please keep to SCJP topics in this forum. We have other Java forums for discussing these more general issues. Moving to Java In General (Intermediate).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic