Game about destroying asteroids
reply to Help
By danpost - about 18 hours ago
You really need to be more specific. Show the two classes involved and point to what you are trying to access and from where. Non-static methods are always called on an object created from the class the method is in. If no object is explicitly given, then the method is being called from within the class (or from a subclass) and the object is the same as that which the calling method was executing on and can be referenced by the keyword 'this'. Calling on a object created by a different class, you must explicitly give a reference to the object and its type must be cast at least to the type of the class where the method resides. To illustrate, from an Actor subclass <Code Omitted>works because the method is in the World class and the object returned is a World type object. Now, let us say you had a getCounter method for your MyWorld objects (MyWorld extends World):<Code Omitted>will not work because there is no method with that name in the World class or any of its super-classes (which, btw, is limited to just the java.lang.Object class). <Code Omitted>would work as the type of the World object returned is cast to where the method is defined. So, basically a method is called using 'objectReference.methodName()' where the type of the reference is adequately specified so that the method can be found.
nx117 wrote...How do you call a method from another class?