Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variables in an interface

 
Kalyan Patti
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Sisco
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic