aspose file tools*
The moose likes Beginning Java and the fly likes interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interfaces" Watch "interfaces" New topic
Author

interfaces

suraj maurya
Greenhorn

Joined: Jan 17, 2013
Posts: 9
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

Joined: May 22, 2012
Posts: 873
    
    5
An interface contains no implementation, just method declarations and constants.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
And welcome to the Ranch
suraj maurya
Greenhorn

Joined: Jan 17, 2013
Posts: 9
Campbell Ritchie wrote:And welcome to the Ranch

thank you
suraj maurya
Greenhorn

Joined: Jan 17, 2013
Posts: 9
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

Joined: Mar 17, 2011
Posts: 8186
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 873
    
    5
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Mar 17, 2011
Posts: 8186
    
  23

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

Joined: May 22, 2012
Posts: 873
    
    5
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

Joined: Mar 17, 2011
Posts: 8186
    
  23

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interfaces