• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interfaces

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface contains no implementation, just method declarations and constants.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
suraj maurya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic