aspose file tools*
The moose likes Java in General and the fly likes Variables in an interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Variables in an interface" Watch "Variables in an interface" New topic
Author

Variables in an interface

Kalyan Patti
Greenhorn

Joined: Jun 27, 2006
Posts: 22
Hi,

I wanted to whether the use of variables in an interface is good practice or not. This question was asked in one of my interview.
Can also explain me the reason if it is NOT a good practice??

Thanks


Kalyan.
Paul Sisco
Greenhorn

Joined: Jun 15, 2008
Posts: 11
Originally posted by Sravan Patti:
Hi,

I wanted to whether the use of variables in an interface is good practice or not. This question was asked in one of my interview.
Can also explain me the reason if it is NOT a good practice??

Thanks


Variables defined in an interface are constants, so they are implicitly public static final variables. They need to be initialized when they are defined.

I could see it being used for something like paymentType for a payment interface, but I would probably just put the constants for that in a base class, not in the interface.



This thread discusses the same issue:
Java Ranch - Instance variables in interface

Paul
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
This may have been a trick question. Your prospective employer may have been trying to find out what depth of trivia you know about the language. All variables declared in an interface are static and final by definition. Given that, can you tell us whether or not you think it is a good practice?
 
 
subject: Variables in an interface