Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface

 
Rambabu Gonela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java In General (Intermediate).

This forum is for servlet questions.
 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you prem and david
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic