Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

having protected variables in interfaces?

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i was wondering can you have protected variables that you

can modify in your classes?

umm, somethin like

 
Alana Sparx
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly sure interfaces only allow you to have public, static and/or final modifiers for methods and primitives.

The compiler should tell you though.....
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't have any fields/variables in an interface, except constants (public static final). You can't have any access modifiers, except public.

Generally, you should not bother writing the "public" in interfaces, as everything is public anyway. Well, perhaps this is a matter of style, but I think so.
 
Vinayak patil
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question related to variables in interfaces:

Why do we have to initialize the variables when we declare them in an interface? And why are they treated as final variables?

Thanks in advance,
Vinayak
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because they are not "variables" -- that's the whole point. They're constants. Interfaces can't include any per-instance data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic