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
posted
0
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 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?