Why are interface variables static and final by default?
From the Java interface design FAQ by Philip Shaw:
Interface variables are static because Java interfaces cannot be instantiated in their own right; 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.
source
Can an interface hold any instance variables?
variables declared in interface are by default public
, static
and final
. Since it is static you cannot call it instance variable.
Interface can have only static variables in JAVA?
Yes, because interfaces cannot be instantiated, so instance state would not make sense.
A quick Google search found the same answer.
http://www.coderanch.com/t/245071/java-programmer-SCJP/certification/final-member-variables-interfaces
Why are interface variables public
Interfaces are "behaviour blueprints". Unlike classes, they shouldn't (and do not) have states.
Since instance variables represents the state of an object, interfaces do not have instance variables (being them private or public).
And since private static variables represent the state of a class, they don't have private static variables either.
Why are all fields in an interface implicitly static and final?
An interface is intended to specify an interaction contract, not implementation details. A developer should be able to use an implementation just by looking at the interface, and not have to look inside the class which implements it.
An interface does not allow you to create an instance of it, because you cannot specify constructors. So it cannot have instance state, although interface fields can define constants, which are implicitly static and final.
You cannot specify method bodies or initializer blocks in an interface, although since Java 8 you can specify default methods with bodies. This feature is intended to allow new methods to be added to existing interfaces without having to update all the implementations. But you still cannot execute such a method, without first creating an instance implementing the interface.
Aside: Note that you can implement an interface with an anonymous inner class:
interface Foo {
String bar();
}
class FooBar {
Foo anonymous = new Foo() {
public String bar() {
return "The Laundromat Café";
};
}
You have to provide the full implementation of the interface for the anonymous inner class to compile.
new Foo()
is initializing the anonymous inner class with its default constructor.
interface variables are final and static by default and methods are public and abstract
As I see it, an interface declares a set of abilities that implementors must have. It refers to the "what" more than to the "how"; It is more a specification than an implementation guideline.
Therefore, methods which are not public are irrelevant in interfaces. Same with non-static data members, which are more related to the specific implementation.
Is public static final redundant for a constant in a Java interface?
Variables declared in Interface are implicitly public static final
. This is what JLS 9.3 says :
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.
Read through the JLS to get an idea why this was done.
Look at this SO answer:
Interface variables are static because Java interfaces cannot be instantiated in their own right; 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.
Related Topics
How to Fix the "Java.Security.Cert.Certificateexception: No Subject Alternative Names Present" Error
Sparse Matrices/Arrays in Java
How to Invoke a Linux Shell Command from Java
Java List.Add() Unsupportedoperationexception
How to Sort List of Objects by Some Property
Keyword for the Outer Class from an Anonymous Inner Class
Why Does Changing the Returned Variable in a Finally Block Not Change the Return Value
Jdbc Connection Failed, Error: Tcp/Ip Connection to Host Failed
How to Load Classes at Runtime from a Folder or Jar
Processbuilder: Forwarding Stdout and Stderr of Started Processes Without Blocking the Main Thread
Why Stringbuilder When There Is String
How to Get Screen Resolution in Java
What's the Difference Between Primitive and Reference Types
Convert Timestamp in Milliseconds to String Formatted Time in Java
Jackson - Deserialize Using Generic Class
How to Convert a String with Unicode Encoding to a String of Letters