permaculture playing cards*
The moose likes Beginning Java and the fly likes Why so much difference in Interface's Variable & method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why so much difference in Interface Watch "Why so much difference in Interface New topic
Author

Why so much difference in Interface's Variable & method

Sajid Reshi
Greenhorn

Joined: Jun 18, 2008
Posts: 3
Why are tha variables in an interface declared as
public, static, final?
Steve McLeod
Greenhorn

Joined: May 26, 2008
Posts: 11
Was it something like this:

public static final String MY_STRING = "HELLO";


Then it's because they are constants.


<a href="http://www.solidsimplesafe.com/" target="_blank" rel="nofollow">http://www.solidsimplesafe.com/</a>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You are correct that interface variables are regarded as constants, but it is nowadays considered poor practice to use them. Create a class which has the same constant fields and import them statically.
Sajid Reshi
Greenhorn

Joined: Jun 18, 2008
Posts: 3
I know that but that was not my question. I meant to say what is the reason for this..
For example the interface methods should have the modifiers public and abstract for inheritance purposes..
so what is the reason for declaring the variables as static, final..

Somebody please help..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
As Steve McLeod has already told you: they are to be used as constants. Java constants are typically declared as public static final fields.
[ June 21, 2008: Message edited by: Campbell Ritchie ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19724
    
  20

Interfaces do not have any implementation details. Since instance and static variables are part of the implementation, they cannot be part of interfaces. Hence the only "variables" that can be part of interfaces are actually constants - static and final. And everything in an interface is public.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ajay Anugula
Greenhorn

Joined: Jun 21, 2008
Posts: 1
An interface is a description of set of methods that confirming implementating classes should have.
Interfaces are supposed to provide only definations and not any implementation, so java is designed so that interfaces can have only constants.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Welcome to JavaRanch, Ajay Anugula
shishir patel
Greenhorn

Joined: Oct 31, 2008
Posts: 1
Originally posted by Rob Prime:
Interfaces do not have any implementation details. Since instance and static variables are part of the implementation, they cannot be part of interfaces. Hence the only "variables" that can be part of interfaces are actually constants - static and final. And everything in an interface is public.


Can someone explain why Java constants (final variables) are NOT part of implementation ?

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why so much difference in Interface's Variable & method