• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variables in an interface

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic