File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface" Watch "interface" New topic


Suri Babs
Ranch Hand

Joined: Mar 27, 2003
Posts: 50
I would like to why i shouldn't declare the private and protected variables in java interface, like the following
public interface BaseInterface{
private int i=0;
protected int j=0;
// Methods declaration
this is the following error i get modifier private not allowed here
private int i=0;
^ modifier protected not allowed here
protected int j=0;
2 errors
billo bailey
Ranch Hand

Joined: Dec 02, 2002
Posts: 50
you should as far as i understand initialize your variables in a constructor, so maybe try

i could be hopelessly wrong but think the initialization should be as above.

only users lose drugs
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
All declared variables in an interface are implicitly public static final. You cannot declare them as protected or private. Even if you just declare them with type alone, which would normally be package-private the compiler will see to it that they are actually public static final.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

An interface cannot declare member variables.
This is an untrue statement, but is the general case. An interface can only declare member variable when they are initialized, public, and final (note that if you do not explicitly make them final the compiler will do so for you). So you can say:

They cannot be private becase there is no class to access them; subclasses cannot access private members. Likewise, they cannot be protected becasus an interface has no subclasses--just implementers.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: interface
It's not a secret anymore!