Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The final field cannot be assigned, for an interface

Tags:

java

interface

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.

like image 421
Matt Avatar asked Dec 14 '10 10:12

Matt


People also ask

CAN interface have final fields?

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!).

Can an interface have static final fields?

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.

Why are interface fields final?

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.

Is it possible an interface to have non-final variables?

No you cannot have non-static variables in an interface. By default, All the members (methods and fields) of an interface are public.


2 Answers

By default fields (member variable) in interface are public static final

and you don't have setter for final

like image 178
jmj Avatar answered Nov 10 '22 02:11

jmj


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.

like image 33
Jon Skeet Avatar answered Nov 10 '22 02:11

Jon Skeet