File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interfaces" Watch "interfaces" New topic
Author

interfaces

Ramana kumar
Greenhorn

Joined: Apr 14, 2004
Posts: 1

Hello my problem here i'm trying to retrive the valu which is assigned in the interface.But i'm getting compilation errors.Cud u pls help me in this regd to retrive value from interface
[ August 30, 2004: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Moved from SCJP
[ August 30, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Welcome to Java Ranch, Ramana.

I'm assuming you are getting the same error as I do:
MyClass.java:18: variable intr might not have been initialized
System.out.println("value of i in interface is " +intr.i);


To fix it you must access the i defined in the interface with MyIntr.i, not through an uninitialized reference to a class implementing the interface.


(Edited MyClass.i -> MyIntr.i thanks to Eddie: see below)
[ August 30, 2004: Message edited by: Barry Gaunt ]
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Should it not be:



An interface is supposed to supply the conditions of a contract that the class needs to meet, not supply constants. There is also the consideration of scope of i; if MyClass.i is renamed, then the i in the interface can be accessed without using the MyIntr prefix.

Hope that helps,

Ed


The nice thing about Standards is that there are so many to choose from!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thank you Eddie, typo fixed. Actually it compiles with MyClass.i too

Regarding the interface providing constants: it's commonly done, see for instance PathIterator.WIND_EVEN_ODD, PathIterator.WIND_NON_ZERO... in the java.awt.geom package. You define constants in an interface because you can implement more than one such interface. If you define your constants in a class you are stuck with inheriting from that class and only that class.

[ August 30, 2004: Message edited by: Barry Gaunt ]
[ August 30, 2004: Message edited by: Barry Gaunt ]
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Barry,

I too have used constants defined in interfaces, but I have learnt from experience that you'd better have a good global file text find function when you need to find which interface the constant is in. I now prefer to put my constants as static variables in appropriate classes so I refer to each one by class.name - gives me more info about where it's defined and how it's used.

Ed
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A few things to consider here...

First, in the main method of your code, you declare an interface called intr -- but that reference is never assigned to anything. (Have you created any objects here? Any use of the keyword "new"?)

Second, you did implement your interface in MyClass. However, you never actually created an instance of MyClass.

Third, variables in interfaces are essentially constants. They are implicitly public, static, and final. So when you do create an instance of MyClass, it will have a final i = 10 (the value you assigned in the interface). BUT...

Fourth, in main, when you define int i = 20; you've declared a local variable, valid only within the scope of the main method. It's important to realize that this is not the same int i that's in MyClass. So when you display "value of i is " + i, you're only referencing the local variable -- not at all touching the int i in MyClass.

Fifth, you can't instantiate an interface, but you can upcast an object that implements the interface.

I hope this helps...


[ September 01, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interfaces