Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes having protected variables in interfaces? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "having protected variables in interfaces?" Watch "having protected variables in interfaces?" New topic

having protected variables in interfaces?

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok, i was wondering can you have protected variables that you

can modify in your classes?

umm, somethin like

You down with OOP? Yeah you know me!
Alana Sparx
Ranch Hand

Joined: Feb 14, 2006
Posts: 121
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

Joined: Oct 30, 2001
Posts: 1970
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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
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,

"I can resist everything except temptation"
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Because they are not "variables" -- that's the whole point. They're constants. Interfaces can't include any per-instance data.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: having protected variables in interfaces?
It's not a secret anymore!