-5

Im preparing for an Interview and have come across Interfaces in Java. As a beginner I couldn't get the use of fields in Interfaces while programming which are static and final as well. I googled it but couldn't find the answer. Sorry if I posted a stupid question. Please answer. Thank you.

  • 1
    possible duplicate of [Java - Why are all fields in an interface implicitly static and final?](http://stackoverflow.com/questions/1513520/java-why-are-all-fields-in-an-interface-implicitly-static-and-final) – gtgaxiola Mar 03 '15 at 16:08
  • 1
    Im not asking http://stackoverflow.com/questions/1513520/java-why-are-all-fields-in-an-interface-implicitly-static-and-final. Its more of usage of fields of Interface. – Manikanta Mahesh Byra Mar 03 '15 at 16:13
  • You don't have to, but if you do then these are fields that make sense to the interface. Fields in interfaces [by default will be static and final.](http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.3.1) Then question becomes why [use constants in programming?](http://stackoverflow.com/questions/2953601/why-use-constants-in-programming) – gtgaxiola Mar 03 '15 at 16:20

1 Answers1

0

I actually think of them as "constants" (static final does not directly imply a constant for any Type in Java), say i have a enum like this:

public enum ModeEnum {
   FAIL_FAST, FAIL_RETRY, HUNDRET_MORE_MODES
}

And a Interface like this:

public interface ISample {

    static final ModeEnum DEFAULT_MODE = ModeEnum.FAIL_FAST;

    public void process(ModeEnum mode);

}

I would just see the "constant" to imply which of the Enum-States is considered the (mostly supported) default.

I must also note that i never see those "constants" in daily business interfaces.

JBA
  • 2,576
  • 5
  • 20
  • 36