• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java In General (Intermediate).

This forum is for servlet questions.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you prem and david
 
A timing clock, fuse wire, high explosives and a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic