Hey guys, I'm having a little trouble working with multiple classes.
Since I'm a noob, I just don't understand it, does anyone have a piece of example code they can give me to help me with calling multiple classes into the main class.
Re: Hey guys, I'm having a little trouble working with multiple classes.
Quote:
calling multiple classes into the main class.
Not sure what you mean by "calling" a class.
Do you mean creating an instance of the class? Use the new statement:
AClass aClsRef = new AClass(); // Create an instance of AClass
To call methods in that class, use its reference variable:
aClsRef.someMethod(); // call someMethod in the AClass instance
What is the "main" class? Is it the first class where execution starts and that has a main() method?
Re: Hey guys, I'm having a little trouble working with multiple classes.
Here's the main class.
Code :
public class OuterSpace extends GraphicsProgram{
public static void main(String[] args) {
new OuterSpace().run();
}
public void run() {
setSize(1200, 800);
}
}
Here's the secondary class.
Code :
public class Asteroid extends OuterSpace implements KeyListener{
public GOval asteroid = new GOval(100, 100, 100, 100);
public void run() {
newAsteroid();
}
public void newAsteroid() {
asteroid.setFillColor(Color.BLACK);
asteroid.setFilled(true);
add(asteroid);
animateAsteroid();
}
How do I call the secondary class to run at the same time in the same application as the main class?
Re: Hey guys, I'm having a little trouble working with multiple classes.
Quote:
How do I call the secondary class to run at the same time in the same application as the main class
Your terminology is confusing.
You don't call classes. You call methods. A method belongs to a class.
You can call methods from within a method.
To call methods in another class, you need a reference to the other class as I said in my previous post.
Can you explain your problem using the above terminology?
From what method in what class do you want to call what method in what class?
Re: Hey guys, I'm having a little trouble working with multiple classes.
I am just looking at these two classes and it may just be my limited knowledge, but your Asteroid class Extends your OuterSpace class and what it sounds like you want to do is have the Outerspace class have an instance of Asteroid. This way Outerspace holds an Asteroid inside of it and not have Asteroid extend Outerspace.