In interface instance variable is by default static and final. if instance variable in interface is final then we must initialize so what is objective to define instance variable in interface or what is use of instance variable in interface???
-
5There's no such thing as an instance variable in an interface. There are constants, basically... – Jon Skeet Sep 29 '15 at 17:23
-
1In other words, they are class variables, not instance variables. – Sotirios Delimanolis Sep 29 '15 at 17:26
-
2possible duplicate of [Why are interface variables static and final by default?](http://stackoverflow.com/questions/2430756/why-are-interface-variables-static-and-final-by-default) – Andreas Sep 29 '15 at 17:43
-
Ya sure there are constant, but my question is what is use of constant class variable in interface – Dharmik Jogi Sep 30 '15 at 17:42
3 Answers
Variables in interface are static final. They are available to all the classes which are implementing it. So to set some parameters which will be used throughout the application can be set here. Rough eg you can set the name of the application which can be used by all the classes.

- 231
- 2
- 14
First of All, Interfaces cannot have instance variables
.
An "instance variable" means an "instance field."
An instance field
is a field that is specific to an individual instance of a class. For example:
class Foo {
// Instance field:
private int bar;
// Static field:
public static final int staticBar;
}
The field bar
is per-instance
, and is not class-wide
. The field staticBar
is class-wide (a static field, sometimes called a "class field").
Interfaces don't have instance fields. They do have static fields. When you do this:
interface FooInterface {
int staticBar;
}
staticBar
is automatically declared public, static, and final.
So that staticBar is roughly equivalent to the one on our Foo class before.
For more help, refer this question
Interface interf{
int i = 10;
}
1) Interface has by default has access modifier. So no object is created for interface for accessing any attributes or component.
2) Any variable define in interface are by default public, static, final - CONSTANT. As a variable is declared as static is can be access without object.
NOTE : No , interface does not contain instance variable.

- 163
- 1
- 7