-2

I understand those fields are public static final by default, my question is on why it's public?

I think my question comes from why interface has fields. Strangely I read from other answers on this site and accepted that they're static final, but why public is confusing for me.

NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
  • Possible duplicate of [Fields in interfaces](https://stackoverflow.com/questions/9446893/fields-in-interfaces) –  Apr 26 '19 at 15:29
  • @jakub_d: I read that, still confused – NeoZoom.lua Apr 26 '19 at 15:29
  • an interface defines a contract, an API, sometimes you need specific constants to be used as part of that interface? –  Apr 26 '19 at 15:37
  • let's try to find some examples, https://docs.oracle.com/javase/8/docs/api/constant-values.html <- these are all the constants in the standard library, at least some of those are in interfaces –  Apr 26 '19 at 15:38
  • https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingConstants.html <- er, this one seems to be used to import convenient constants into multiple classes? eww –  Apr 26 '19 at 15:39
  • https://docs.oracle.com/javase/8/docs/api/java/awt/Transparency.html <- this one appears somewhat sane –  Apr 26 '19 at 15:43
  • @jakub_d: Regard to your [constant-values-link](https://docs.oracle.com/javase/8/docs/api/constant-values.html), those are all interfaces? – NeoZoom.lua Apr 26 '19 at 15:43
  • not all of them, most are in regular classes, as I say "at least some are in interfaces" –  Apr 26 '19 at 15:45
  • @jakub_d: Can I say that interface should not involve what should be used in implementation, so its fields are public by default? – NeoZoom.lua Apr 26 '19 at 15:51
  • If we ignore the java 8 default methods feature, how would you use a non-public field in an interface anyway? –  Apr 26 '19 at 15:53
  • @jakub_d: I never think of it before... so private means it can only be used from inside? – NeoZoom.lua Apr 26 '19 at 15:56
  • @jakub_d: Maybe I need a reason about why not private instead of why it's public... – NeoZoom.lua Apr 26 '19 at 15:59
  • private can only be seen from the class itself and traditionally (before 8) interface methods had no implementation so no one would get to see the field –  Apr 26 '19 at 16:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192445/discussion-between-jakub-d-and-li-see-lei-cow-q-q). –  Apr 26 '19 at 16:00

2 Answers2

1

Interfaces are meant to be implemented by other classes, not to encapsulate any data. This is why all fields are public.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Alex Mi
  • 1,409
  • 2
  • 21
  • 35
1

Relevant bit of spec: https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.3

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

You are only allowed to redundantly affirm that you want public, trying to change it results in a compile-time error.

Private would be almost completely pointless (the implementing class would not be able to see the field). If you say nothing you get public instead of the usual "package" visibility. And you are prevented from specifying protected. Not sure if there is a valid use-case for wanting that.

  • Just a (and last) little question: now interface can have default methods, so it sounds like `private static final` fields of an interface can be used in them...? – NeoZoom.lua Apr 26 '19 at 16:46
  • @LiSeeLeiCow-Q__Q private fields in interfaces are still disallowed (see above), if they were allowed, they would be usable from a default method. And possibly through reflection. That's why I said *almost* completely pointless. :) –  Apr 26 '19 at 17:33
  • 2
    So the core of the answer is that the designers of the language made it so. You can come up with convoluted scenarios where it would make sense to have a non-public field, but the designers of the language wanted to encourage only this specific usage. –  Apr 26 '19 at 17:43