what is the difference between constants and instance variables in java?Which one of the them is allowed to be defined in an interface? Examples would help.
Asked
Active
Viewed 91 times
-6
-
Have you done your research? – reto Apr 24 '15 at 10:29
-
2[constants variables](http://stackoverflow.com/questions/66066/what-is-the-best-way-to-implement-constants-in-java), [instance variables](http://stackoverflow.com/questions/16686488/java-what-is-an-instance-variable) and [your question](https://www.google.de/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=java+instance+variable+interface) The first link right out gives you the answer... – SomeJavaGuy Apr 24 '15 at 10:31
1 Answers
0
With respect to Interface. Any "field" you declare in an interface will be a public static final field. In other words, a constant. There is no instance variable in interface If you try to make it protected, it will fail because it would become protected static final, which is not allowed inside interfaces.
If you see something like this and is confused
public interface SampleInterface
{
int i = 123;
public void display();
}
The Java Complier makes that code like this
public interface SampleInterface
{
public static final int i = 123;
public abstract void display();
}
Every field in an interface is public, static and final, even if you omit one or more of the modifiers.
Every method in an interface is public and abstract, even if you omit one or more of the modifiers.

Ankur Anand
- 3,873
- 2
- 23
- 44