File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variables in interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Variables in interfaces" Watch "Variables in interfaces" New topic
Author

Variables in interfaces

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.
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
Please post it somewhere else. This forum is for SCJP certification.


Swapnil S. Sonawane<br /> <br />B.Tech (Expected May 2009)<br />Computer Science, NIT Durgapur, India<br />SCJA 1.0
Kalyan Patti
Greenhorn

Joined: Jun 27, 2006
Posts: 22
Hi,

I am really sorry about the post at wrong place.

Thanks
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

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 ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Zaheer Ahmed
Greenhorn

Joined: Feb 06, 2007
Posts: 22
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...


Zaheer Ahmed<br />SCJP 5
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
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


Ben,
--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
Kalyan Patti
Greenhorn

Joined: Jun 27, 2006
Posts: 22
Hi,

Thanks Zaheer Ahmed and Benz Zaidi. My doubt is clear now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variables in interfaces