简体   繁体   中英

How to transfer a variable from main method class to another class?

Okay... in my main class, I ask the user to enter an input int value. Now I need to take this value to a different class to do some coding. How?

Code from main class:

neededValue = keyboard.nextInt();

object.methodName(xxxx, yyyy):

Code from a different method in a different class:

public void methodName(double xxxx, int yyyy) {

        int index;

        for (index = 0; index < array.length; index++) {
            xxxx = array[index].xxxx;
            if (xxxx > **neededValue**) {
                //some more code

            }

        }
    }

Lets say user input is 4, how can I transfer that "4" to other classes?

You need to pass neededValue as argument

neededValue = keyboard.nextInt();   
object.methodName(xxxx, yyyy,neededValue):

Then in methodName function

public void methodName(double xxxx, int yyyy, int neededValue) {

    int index;

    for (index = 0; index < array.length; index++) {
        xxxx = array[index].xxxx;
        if (xxxx > **neededValue**) {
            //some more code
        }
    }
}

Hope it helped.

From main:

neededValue = keyboard.nextInt();

object.methodName(xxxx, yyyy, neededValue):

From the class:

public void methodName(double xxxx, int yyyy, int neededValue){
    int index;

    for (index = 0; index < array.length; index++) {
        xxxx = array[index].xxxx;
        if (xxxx > **neededValue**) {
            //some more code
        }
    }
}

Add a parameter to your another class method and pass the value from your main class.. same as below:

class A{
...
neededValue = keyboard.nextInt();

DifferentClass object = new DifferentClass();
object.methodName(xxxx, yyyy, neededValue):
...
}

class DifferentClass{
public void methodName(double xxxx, int yyyy, int neededValue) {
...
}
}
  1. You can pass the nextValue variable to the method directly, like:

object.methodName(xxxx, yyyy, nextValue)

  1. or you can declare a int variable in the class A of object:

    int someValue;

and set it by constructor or setter

object = new A(nextValue);
or object.setNextValue(nextValue)

then, your invoke object.methodName(xxxx, yyyy) will work with the 'someValue' field inside the object

Multiple ways to do it, few are listed below,

i) Create parameter in your method (explained in other solutions).

ii) Create a variable in your class with get/set methods in your class (explained in other solutions).

iii) Create a static variable in your class (explained below)

You can have a static variable in your class,

private static int neededValue;

And in your main, you can assign the value for it.

ClassName.neededValue = keyboard.nextInt();

object.methodName(xxxx, yyyy):

And you can use directly in your class method,

public void methodName(double xxxx, int yyyy){
int index;

for (index = 0; index < array.length; index++) {
    xxxx = array[index].xxxx;
    if (xxxx > ClassName.neededValue) {
        //some more code
    }
 }
}

Disclaimer: Know more about static keyword before using it.

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