File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Which is better (Accessing constants defined in interface) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Which is better (Accessing constants defined in interface)" Watch "Which is better (Accessing constants defined in interface)" New topic
Author

Which is better (Accessing constants defined in interface)

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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


SCJP, SCWCD, SCBCD
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Which is better (Accessing constants defined in interface)
 
Similar Threads
Which is better? Declaring constants in a class or Declaring them in interface?
Is a CONSTANT to be both final and static?
Interface with ONLY CONSTANTS
Enum vs Constant File
Question for Bear Bibeault about ccc.jar