This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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
jQuery in Action, 3rd edition