Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why so much difference in Interface's Variable & method

 
Sajid Reshi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are tha variables in an interface declared as
public, static, final?
 
Steve McLeod
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was it something like this:

public static final String MY_STRING = "HELLO";


Then it's because they are constants.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ajay Anugula
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Ajay Anugula
 
shishir patel
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic