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 variables. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface variables." Watch "Interface variables." New topic

Interface variables.

Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Hi All.
I have a performance related question regarding interface variables.
Consider the following interface...
interface I { // First case
int i=0;
Since all variables declared in an interface are implicitly 'public static final', is it better to specify this explicitly as in the following?
interface I { // Second case
public static final int i=0;
My hunch is that there would be no problem or overheads with the Second case since final variables are compile time constants & do not have any effect during Runtime.
Pls. give your comments on above.
- Himanshu

Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Himanshu Jhamb:
Since all variables declared in an interface are implicitly 'public static final'...

Based upon the fact that any member variables in an interface are implicitly static and final, I would assume that writing your code in either way would result in the same bytecodes being produced.
Indeed, I just compiled the following code:

After using javap to look at the bytecode, I see this was produced:


SCJP Tipline, etc.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Himanshu,
Performance-wise, it probably makes no difference at all. But explicitly declaring interface variables as public static final is better coding style since even novices (who might not know this implicit rule) can see your intent.

Hope this helps
Michael Morris

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
I agree. Here's the link:
subject: Interface variables.
It's not a secret anymore!