Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interfaces

 
Ramana kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved from SCJP
[ August 30, 2004: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic