This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interface" Watch "Interface" New topic
Author

Interface

Rambabu Gonela
Greenhorn

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;
}

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

Please .. give me explanation ok bye
Ben Souther
Sheriff

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.

Ex:
int i = 10; //Will Compile

int i; // Do not Compile.

Regards
Prem
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
Greenhorn

Joined: Jul 31, 2006
Posts: 24
thank you prem and david
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Interface
 
Similar Threads
Array Question
initializers-referencing
Final Members Doubt
Why final constants won't take the default values?
final instance variables