• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

variable definitions within an interface

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Will somebody please put me straight on the following issue ?
What are the valid modifiers for variables declared within an interface definition ?
I've read that "The only fields allowed in an interface definition are contstants that are declared both static and final" - page 113 of O'Reilly's 'Java in a nutshell'.
And yet the following simple code compiles OK ???
public interface MyInterface {
char c = 'c';
public void method1();
public void method2();
}
What am I missing, is the variable implicitly assumed to be a constant ( static final) ?
Also, how deeply does the programmer exam delve into the declaration and use of interfaces ?
Cheers, Mark.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Lees:

1) I've read that "The only fields allowed in an interface definition are contstants that are declared both static and final" - page 113 of O'Reilly's 'Java in a nutshell'.
2) What am I missing, is the variable implicitly assumed to be a constant ( static final) ?
3) Also, how deeply does the programmer exam delve into the declaration and use of interfaces ?
Cheers, Mark.


1) that is very true.
2) Yes, in interface member variables are by default(implicitly) coderanch, static & final
if you remove initialization from your code then it won't complile.
3) I am also preparing and what i found in mock tests that it looks important point.


------------------
Regards
Ravish
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mark Lees,
Read the following points..

All members (attributes & methods) of interface are implicitly coderanch.
All attributes defined in interface are implicitly static and final.
All methods defined in interface are NOT static.
All methods defined in interface are implicitly abstract..

All classes defined in an interface are implicitly static
Inner interfaces are implicitly static, no matter you put static modifier or not.
Outer interface are NOT static, just like outer class.
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
one small question
if a variable is defined in an interface say int value=10;
can it be used in a class that implements it in the way given below
int value=10;// i.e.redeclaring it in the class is allowed
or
int value=11;// will this give a compiler error
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha
If you redefine the variable in the clas that implements the interface what you'll end up doing is hiding the variable in the interface. In order to access the variabe defined in the interface you'd have to specifically access that variable.
Look at the code below:

hope that clears it up for you.

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic