Inteface Doubt
2 posts in topic
Flat View  Flat View

Posted By:   uma_sankar
Posted On:   Wednesday, March 29, 2006 02:18 AM

why should instance variable in interface is always final?

Re: Inteface Doubt

Posted By:   Anonymous  
Posted On:   Tuesday, April 4, 2006 12:07 AM


Its really nice doubt..

First of all, the interface can not
have any state (i.e, instance variables), reason is as follows.

Before we dick deeper in to this
discussion we need to brush up our OOPs statements now,

1. Class has state and behavior.

State is a value holder , it's value
will change.[i.e instance variable]

Behavior means how the class should
act when some action triggered to the class[i.e methods

So, if any entity (in opps) which
has above two behavior can be act as class.

Now, I'm coming to your doubt.

First you have to understand one thing
that is, Interface is not a class. So it should not have any
state(i,e instance variable) and it's own behavior ( as you know,
interface does not have any method implementation, only it has method
template, class which is gonna be implement it will have to write a
implementation code for the method template)

so, If interface allow us to declare a
non-final variable, it'll be act as a instance variable. Thats why
the interface's fields are always final.[as you know, final variables
are not a instance variable as per OOPS]

[Interfaces are just a interface among
its implementations, since its not a class is should not have any
state and behavior]

Friend i jst gave some abstract
information, if you refere come opps related books you can get more
clear-cut idea about this topic.

my friend, hope i have clarified your

if not, feel free to ask me..

Re: Inteface Doubt

Posted By:   Almagest_FUTT  
Posted On:   Wednesday, March 29, 2006 07:20 AM

You can't declare instance variables in an interface. Whether you specify it or not, any variable declaration in an interface is static and final.

The reason is that's how it's been defined: http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.3.
About | Sitemap | Contact