• 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

Variables inside Interface

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that all variables (i.e. data members) inside an interface are final and static. Going by this logic, the following program should throw an error, but works perfectly! Kindly clarify why?
interface inter1
{
int i=10;//i is final?
void dis();
}
interface inter2
{
int i=20; /* Reinitializing i again here. Compiler is expected to throw an error.*/
}
class face implements inter2,inter1
{
public void dis()
{
System.out.println("Interface 1 is called");
}
public static void main(String ar[])
{
face ob=new face();
ob.dis();
System.out.println(inter1.i);
System.out.println(inter2.i);
}
}


------------------
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would guess the compiler is making the same assumption about interface variables you're making: they're all static and final. Hence, there's no need to include those modifiers. The same thing occurs with interface method scope: they're always public.

------------------
Brent Worden
http://www.Brent.Worden.org/
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic