Doubt in interfaces...

interface I{

int f(int x);

int var = 4;

}

publicclass Cimplements I{

publicint f(int x){

return x * x;

}

publicstaticvoid main(String[] args){

try

{

I[] a =new C[I.var];

System.out.println(a.length);

System.out.println(a[0]);//output - null

System.out.println(a[1]);//output - null

System.out.println(a[2]);//output - null

System.out.println(a[3]);//output - null

for (int i=0; i<a.length; ++i)

{

System.out.println(a[i].f(i));// Null pointer exception

}

}catch (Exception e){

System.out.println("Error: "+e.getMessage());// error in this line

e.printStackTrace();

}//catch

}//main

}//class

question: i have to call f() which is in interface a.length times. how?>

[2152 byte] By [dhamu123a] at [2007-11-27 4:30:53]
# 1

This has all of nothing to do with interfaces.

You haven't initialized any of the slots of the array a with actual objects. They are all null (as you see when you printed out so I don't really know what you are thinking).

Please direct yourself to http://java.sun.com/docs/books/tutorial/java/index.html

cotton.ma at 2007-7-12 9:40:15 > top of Java-index,Java Essentials,New To Java...