I have an object whose functions I would like to call from with in another class for example
class smo {
int spoon = 10;
smo() {
}
int get_spoon() {
return spoon;
}
}
class boat {
boat() {
}
print_smo(Object test) {
test.get_spoon();
}
}
it tells me that the function get.spoon() does not exists. The error makes sense since the object hasn’t been created the function can’t be called, but it will exists when its run and I have passed an appropriate object of the type smo to it.
Since Java has a static syntax check it needs to know the right type of your objects before running the program. And since it doesn’t have any kind of type inference it’s a programmer’s duty to declare them in source code.
This means that to actually call the
smo‘s methodget_spoon()on asmoobject you have to declare that it will be of that type and not just anObject(which is the least specific type possible inJava):this will work.. and will let you to call
oneBoot.print_smo(new smo()).Two side notes:
ClassNamemyLongVariable