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.

Results 1 to 5 of 5

Thread: Hey guys, I'm having a little trouble working with multiple classes.

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default 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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hey guys, I'm having a little trouble working with multiple classes.

    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?

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: Hey guys, I'm having a little trouble working with multiple classes.

    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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hey guys, I'm having a little trouble working with multiple classes.

    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?

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Alright guys, I am having some serious trouble with this one.
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 18th, 2011, 04:36 PM
  2. Hey guys, my While loop isn't working correctly.
    By metanoia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 26th, 2011, 11:15 PM
  3. Dice Wagering Game new to Multiple Classes
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 16th, 2011, 08:22 PM
  4. help with using multiple classes
    By finalfantasyfreak15 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 5th, 2011, 12:18 AM
  5. [SOLVED] Trouble accessing variables from other classes
    By Elementality in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 5th, 2011, 11:50 AM