assigning a value inside interface....
I have the following code...
Interface test
{
int i;
}
it shows the error when i compiled it. Then i assigned a value for i=0. After this correction it's working fine.
My doubt is "Why we assign a value ?". Because in interface it is defaulty static and final. if i use a static field in my class then it is automatically assigned for "0". But here it shows error ? Why this error will come ?
----------------
suresh
Re: assigning a value inside interface....
In java, final variables are not assigned a default value. As you have pointed out, variables in interfaces are implicitly final.
Hence, you have to assign value mandatorily.
Re: assigning a value inside interface....
Hi suresh,
In interface what ever values you will declare tht will ct as a final values. This values will be applicable for your entire application.
Thanks,
vishnu.
Re: assigning a value inside interface....
Hi,
The answer is itself in your's question. You said that all the variables of an interface are automatically static and final. Every final variable need to be assigned some value in class or interface, otherwise it will through compilation error. Please follow the blow code :
interface test
{
final static int i; // compilation error.
}
or
interface test
{
static int i; // compilation error.
}
or
class test
{
final static int i; // compilation error.
}
so it should be like this:
interface test
{
final static int i = 3.49; // No compilation error.
or
int i = 3.49;
}
or
class test
{
final static int i = 3.49; // No compilation error.
}
class test
{
static int i; // No compilation error.
}
Hope you will be agree with me.
Thanks
Bhagat Singh
[QUOTE=psuresh1982;7294]I have the following code...
Interface test
{
int i;
}
it shows the error when i compiled it. Then i assigned a value for i=0. After this correction it's working fine.
My doubt is "Why we assign a value ?". Because in interface it is defaulty static and final. if i use a static field in my class then it is automatically assigned for "0". But here it shows error ? Why this error will come ?
----------------
suresh[/QUOTE]
Re: assigning a value inside interface....
hi ,
what ever variables you declare in interface that variable are static & final variables it will ask declaration that's why we need to provide variable value at the time of declaration.
Thanks,
vishnu.