简体   繁体   中英

How do I call a variable from the public static void main to another class?

I am truly at a loss I have been searching everywhere. I have been given this code

public class Main
{
public static void main(String args[])
{
  int target = (int) (Math.random() * 1000);

    System.out.println("The number is " + target);

}

}

They want me to create a class Finder that takes the target and uses it in a for statement. my problem is I can not figure out how to pull target from this code. I am a student and the book we are using is no help. I have tried calling it with

numberSearch = Main.target
numberSearch = Main.main.target
numberSearch = Main.main() 

and many others.

您不能在方法外部访问局部变量

You said "create a class Finder that takes the target", which means that the Finder class should accept the target value as a parameter, either on the constructor, or on the method that performs the find.

// Using constructor
public class Finder {
    private int target;
    public Finder(int target) {
        this.target = target;
    }
    public void find() {
        // perform find of 'this.target' here
    }
}

// On find method
public class Finder {
    public void find(int target) {
        // perform find of 'target' here
    }
}

First off, lets order your code using tab

public class Main
{

    public static void main(String args[])
    {   
        int target = (int) (Math.random() * 1000);

        System.out.println("The number is " + target);
    }

}

Now what you want to do is create an object of the class you say you need to use in school (Finder). Lets make the identifier for this object "obj".

public class Main
{

    public static void main(String args[])
    {   
        Finder obj = new Finder();
        int target = (int) (Math.random() * 1000);

        System.out.println("The number is " + target);
    }

}

Now that we have done this you have to create a method in the class Finder that takes in an integer (so it can take in the variable you call target since it is an int itself). And example of a method like this that uses the variable target in a for-loop would be:

public void forLoopMethod(int target)
{
    //Some sort of for loop involving the int target goes here:
    for()
    {

    }
}

And then simply calling the method forLoopMethod in your class called Main

public class Main
{

    public static void main(String args[])
    {   
        Finder obj = new Finder();
        int target = (int) (Math.random() * 1000);

        obj.forLoopMethod(target);
        System.out.println("The number is " + target);
    }

}

Your problem is that you are trying to find a local variable in a separate class. Variables can be set outside of a method like main(), and if they are public variables, they can be easily accessed by another class.

Outside of the main() function, inside the Main class, you would place the numberSearch variable. Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

Be aware that if your professor wants you to use a private variable, you will need to use getters and setters.

/*How I tested the code*/
public class Main {
    public static int numberSearch;
    public static void main(String args[]) {
        int target = (int) (Math.random() * 1000);
        numberSearch = target;
        Finder.getResult();
    }
}

/*in a separate file*/
public class Finder {

    public static void getResult() {
        int t = Main.numberSearch;
        System.out.println(t);
    }

}

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