File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General 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 » Java in General
Bookmark "Interface" Watch "Interface" New topic


Rambabu Gonela

Joined: Jul 31, 2006
Posts: 24
all variables declared in interface are by defualt public static and final ..ok but in interface why compulsary we have to assign value to variable... its already static... in class by default it will take value as 0.
but why in interface giving error(already its static variable so its should take value as 0 by default, but its not happening)

interface I
int i;

C:\Rambabu>javac I.jav = expected
int i;
1 error

Please .. give me explanation ok bye
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Moving to Java In General (Intermediate).

This forum is for servlet questions.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
It needs to be initialized because it is final. Final variables needs to be initialized either while declaring or in the constructor. Interface do not have constructors. So it has to be initialized during declration.

int i = 10; //Will Compile

int i; // Do not Compile.

David Nemeskey
Ranch Hand

Joined: Nov 08, 2006
Posts: 52
Final fields have to be initialized in the initializer, or in a constructor.

Static finals can only be initialized in the static initializer:

Final fields can be initialized either in the initializer or in a contructor:

An interface cannot have initializers or constructors, so you can only use the first form.
Rambabu Gonela

Joined: Jul 31, 2006
Posts: 24
thank you prem and david
I agree. Here's the link:
subject: Interface
It's not a secret anymore!