• 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

why variables inside interface has to final

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables inside interfaces has to be public static and final. public because anything inside interface has to be visible to outside world. static because we cannot instantiate interface. but why it has to final ??
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shivendra
Looking on the Java Website it says:

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces


The pertinent bit of the above quote is where it mentions constants.
If in your interface you didn't use 'final' then it would be possible to change the value of the object when ever you felt like it, therefore you would be storing a variable rather than a constant.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply James,

I know that interface can contain only constant that's why we make variable as static final. My question is why only contants are allowed.


therefore you would be storing a variable rather than a constant.



I am afraid I couldn't understand your above above mentioned statement.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I suppose if you allowed variables and not just constants, then it wouldn't be an interface. If you want to change the values of variables use variables defined inside a class. Interface exists for a certain reason, and variables is not part of that reason, variables within interfaces don't really add anything to the picture. Think of the reasons why you use interfaces, and ask yourself if you can see a situation where it would make your life as a programmer easier if interfaces included variables.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm wondering if it's even necessary to allow constants. Joshua Bloch dont think so.

Use interfaces only to define types
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me put forward a simple example :


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shortly, interface is a specification. it can not contain any implementations
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interfaces are used in java to impose or to provide some functionality to the user.

means if want to force user to do something which will be helpful for user only or to stenderzie the code for all we will develop interfaces.

so anything we will write inside the interface is to provide flexibility for the users at the same time to maintain uniqueness among the code we have the things inside the interface which user can user for flexibility but can't modify to achieve the uniqueness in the code architecture.

so because of this only by default member variables are final in an interface...

you can use but can't be able to change the value.....

interfaces are something like propretry softwares you can use the things but can't change the things.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic