简体   繁体   中英

How can I pass an object onto another class's method in my java code?

PROBLEM

How can I pass an object onto another class's method in java? When I call a method from class1's code that's in class2 my finch robot connects for a second time. As you can see in the console the robot says "Connecting to finch" at the start of calling the method. This keeps going in and endless loop and doesn't execute what I wanted to do with the robot (make it turn blue). I was wondering how I can pass an object onto another classes method to fix this? Below I got told an helpful tip.

HELPFUL TIP I GOT TOLD

"Your initialising the finch twice for some reason. Basically what you need to do is put a Finch object in the constructor of your method as opposed to initialising it again"

CONSOLE

Console

Code for class 1

package class1;

import java.util.Scanner;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.awt.Color;

public class class1 {
    public static Finch red = new Finch();

    public static void main(String[] args)  {

        red.setLED(Color.red);
        System.out.println("eedde");
        System.out.println("xssccsccscdcddcdccdcdcdcdc");
        System.out.println("eedde");
        System.out.println("eedde");
        System.out.println("eedde");
        class2.class2test(); // this works the method is called but it's the robot in class2 that's the issue. 



    }
}

Code for class 2

package class2;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import edu.cmu.ri.createlab.terk.robot.finch.Finch;

public class class2 {
    public static Finch red = new Finch(); //I will need to remove this when passing the object through 
         public static void main(String[] args)  {

        red.setLED(Color.red);

}

    public static void class2test() {
        System.out.println("CLASS2");
        red.setLED(Color.blue); //this doesn't get executed
    }
}

First off, you have two main methods for some reason which should not be necessary unless you were just using them to test the Class or something. The main is simply where the program will begin . Each time you run the program it will only begin one time, so you shouldn't need two normally.

To answer your question, to pass an Object to a method, you simply need to add it to the parameter of the method (between the parenthesis):

public static void class2test(Finch red) {
    System.out.println("CLASS2");
    red.setLED(Color.blue); 
}

Notice how it now requires a Finch to be passed to it to be called, which it will name red to be used within the method. You can also remove the public static Finch red = new Finch(); line in class2 now, as it is now not needed.

Now here is a sample main similar to yours to show how you call the method:

public class class1 {

    public static Finch red = new Finch();

    public static void main(String[] args)  {
        class2.class2test(red);
    }
}

Notice how you now need to put red inside of the parenthesis in order to pass the Finch you created as a class variable. Note that the name you used in class1 does not need to match the name of the parameter for the method in class2 .

Unrelated Note - I also recommend you look up proper Java naming conventions, you should name a class WithCasingLikeThis , not with a lowercase letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM