Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces

 
Eric Lidell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are interfaces implicitly static ?
class x{
interface y{}
class m{
interface l{}
}
}
While compiling this code,i am getting a message saying that interface l is implicitly static.but no error for interface y.
what is the meaning of static when applied to classes and interfaces?
for the matter ,what is the meaning of final when applied to innner classes?
 
sherry cuenco
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterfaces are implicitly abstract. In your example, you're declaring an interface inside a class.
Static modifier can be applied to instance variables and methods. These variables and methods belong to the class and not the instance of the class. So, if that class (with static members in it), is instantiated many many times, the static variable is shared by all the instances. They share only one copy.
Static modifier when applied to interface? Interface methods cannot be static, only public and abstract. Interface variables are declared public, static and final.
Hope this helps
[ August 07, 2003: Message edited by: sherry cuenco ]
 
Eric Lidell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why does the compiler show error for interface l and not for y?
class x{
interface y{}
class m{
interface l{}
}
}
if i replace the same code as follows
class x{
interface y{}
static class m{
interface l{}
}
}
it works fine.
Whats the reason?
why is an interface within a nested class implicitly static ?
any reason why?
 
Cody Beth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Simon,

In the above example interface y and interface I are member interfaces .
and member interfaces are always implicitly static so they are never considered to be inner classes.
And an inner class can never refer to static members (i.e non-final members)
So inner class m cannot refer to interface I

But in ur second example ..

class m is static nested class that can access static members and hence can access interface I.
for details refer to :
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#270924

Regards,
Cody
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch "Cody" and thanks for paticipating in this forum. Please take a few moments to review JavaRanch's Naming Policy and change your displayed name accordingly. Thanks - Barry(Moderator)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic