-2

I am new to Java. I heard that Interface can have only static variables, is that correct?

3 Answers3

0

This is correct. The reason is that Java interfaces cannot be instantiated by themselves. You must instantiate an instance of a class that implements the interface. Because of this, the value of the variables must be assigned in static context because in this case no instance will exist.

They also need to be declared as final to ensure that the value assigned to the interface variable is a true constant that cannot be changed at run-time by other program code.

Matt Jones
  • 511
  • 3
  • 12
0

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

Brandon
  • 9,822
  • 3
  • 27
  • 37
0

As mentioned in the comment

public static final variables

1. 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. 

2. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.
sTg
  • 4,313
  • 16
  • 68
  • 115