This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interfaces

 
suraj maurya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The data members in an interface are by default static and final. Why they are made so? please make it clear to me.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface contains no implementation, just method declarations and constants.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
suraj maurya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And welcome to the Ranch

thank you
 
suraj maurya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:An interface contains no implementation, just method declarations and constants.

thank you...but if interfaces are only made for the declaration of methods and constants then they should have made data members final only..why static???
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj maurya wrote:
Ivan Jozsef Balazs wrote:An interface contains no implementation, just method declarations and constants.

thank you...but if interfaces are only made for the declaration of methods and constants then they should have made data members final only..why static???

Because you can't instantiate an interface. Therefore there is no 'instance'; so the only thing left is the interface itself.

Indeed, there are some who think they shouldn't have included constants at all, since their purpose is to define behaviour; but I think that's maybe a little too purist. I certainly agree with Josh Bloch that you shouldn't use them only to define constants though.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Indeed, there are some who think they shouldn't have included constants at all, since their purpose is to define behaviour; but I think that's maybe a little too purist


But the behaviour can relate to facets like the possible values of a parameter.
If I have

then I need some guidance what to pass as the status parameter, and interface-defined constants can be useful here.
Moreover the best place to define these constants can be along with the method, that is, in the same interface.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:
then I need some guidance what to pass as the status parameter, and interface-defined constants can be useful here.

parameter as constant(in interface) ? in some cases though like return value
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:then I need some guidance what to pass as the status parameter, and interface-defined constants can be useful here.
Moreover the best place to define these constants can be along with the method, that is, in the same interface.

In general, I agree, especially for numeric constants like π; but purists might say that you're defining a global constant, which might run you into problems later on - for example, when testing.

After all, in an interface, what's the difference between:
public double pi;
and
public double pi();
?
Some might argue that the latter is actually more flexible...on the other hand, the counter-argument is that it's also more error-prone.

I'm not advocating "constant" methods you understand; just trying to show that there is an alternative.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

After all, in an interface, what's the difference between:
public double pi;
and
public double pi();
?


A constant should be a constant, and the compiler can (should and actually does) infer the value of a compile time constant.

 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:A constant should be a constant, and the compiler can (should and actually does) infer the value of a compile time constant.

I hope you're not saying we should program according to what the compiler does internally.
And as I say, I'm certainly not advocating the use of constant methods; especially in a case like above.

I have, however, had cause to use them in order to produce a typed constant - and then they were defined in the relevant interface.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic