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.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
Not sure what you mean by "calling" a class.calling multiple classes into the main 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?
Here's the main class.
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.
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?
Your terminology is confusing.How do I call the secondary class to run at the same time in the same application as the main class
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?
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.