Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variables in interfaces

 
Kalyan Patti
Greenhorn
Posts: 22
  • 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
 
Swapnil Sonawane
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post it somewhere else. This forum is for SCJP certification.
 
Kalyan Patti
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am really sorry about the post at wrong place.

Thanks
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is scjp forum you can discuss it at another place

[ June 20, 2008: Message edited by: Ninad Kulkarni ]
[ June 20, 2008: Message edited by: Ninad Kulkarni ]
 
Zaheer Ahmed
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sarwan!

The only kind of variables that you can define within an interface are "public static final" by default if you even specify it or not.

The answer to your question is in interface we can only define constants but no instance variables. The constants make sense in the interface as long as you those constant do specify something about the interface itself.
But we can never have any instance variables or non-final and non-static and non-public variables in an interface declaration.

Hope this helps...
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a good programming practice till those variables are related to interfaces themselves. As Zaheer quoted earlier, there is no ways that
we can declare instance variables inside an interface, only constants.
But declare only those contants which are interface specific, not
related to something else. Otherwise it will really chew the clarity of your program up .

Ben
 
Kalyan Patti
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks Zaheer Ahmed and Benz Zaidi. My doubt is clear now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic