I have a class Product and an interface LargeProduct. Product implements LargeProduct.
LargeProduct has a variable height which has getter and setter methods which have to be implemented in the Product class.
The height variable in LargeProduct is defined like so:
public int height = null;
The getter method works fine:
public int getHeight() {
return height;
}
But the setter method does not:
public void setHeight(int height) {
this.height = height;
}
Errors:
The final field LargeProduct.height cannot be assigned
The static field LargeProduct.height should be accessed in a static way
I'm not sure which error it's actually giving.. I'm using Eclipse and when I hover it gives the first error, and at the bottom of the window it gives the second error.
Fields must be final, as Adriaan said, because an interface is, and should, be stateless. An interface with a state basically should be an abstract class. If you have a public static field that is not final , findbugs will complain (rightly!).
Type of variables: Abstract class can have final, non-final, static and non-static variables. The interface has only static and final variables. Implementation: Abstract class can provide the implementation of the interface. Interface can't provide the implementation of an abstract class.
Interface variables are static because Java interfaces cannot be instantiated on their own; the value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.
No you cannot have non-static variables in an interface. By default, All the members (methods and fields) of an interface are public.
By default fields (member variable) in interface are public static final
and you don't have setter for final
Interfaces can only include constants, not general-purpose variables. Interfaces should only contain constants if they're genuinely relevant to the rest of the interface, and they should use SHOUTY_CASE
as per any other constant. It sounds like LargeProduct
shouldn't have a constant called height
- but instead, your implementation should declare the height
field.
Note that interfaces are meant to be APIs, showing the abilities of types. Fields shouldn't be part of the API - they're an implementation detail. After all, who's to say that you'll write getHeight
and setHeight
based on a simple variable? Those methods could query a database, or delegate to an instance of some other type. The consumer of the interface shouldn't know or care about that.
For further information about fields in interfaces, I suggest you read section 9.3 of the Java Language Specification:
Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With