0

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???

3 Answers3

0

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.

vivek
  • 231
  • 2
  • 14
0

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

Community
  • 1
  • 1
Adil
  • 817
  • 1
  • 8
  • 27
0
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.

Vishal Sheth
  • 163
  • 1
  • 7