Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interfaces can have variables!

801484Sep 22 2008 — edited Sep 23 2008
Well some of eminent counsellors force me to accept that Interfaces cannot have variables....
no ...never ....Not At All:(

But when i encounter to this tutorial(*In support of my belief as i know already*)

[http://java.sun.com/docs/books/tutorial/java/IandI/interfaceDef.html]

Interfaces can have variables but they must be static and final OR they should be constants (statically declared).

just to test myself i am running this code where an int variable is declared inside an interface...it works fine...
why?

Here I am not declaring any method inside interface also i am not implementing that interface still it gives output?
interface A
{
    int b=7;
}

public class TestInterface
{
public static void main(String args[])
{
    System.out.println("value "+ A.b);
}
}
So either the tutorial is false or the java compiler do not know about the Interfaces (MayB interfaces are treated as classes internally because a .class file is created for each Interface)

OR this bug (If this is a BUG) is not fixes even in jdk1.5_16 (which i used)

OR my approach is wrong completely.

please Help me.
Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 21 2008
Added on Sep 22 2008
30 comments
706 views