0

I'm a Java newbie and I have this question.

Can I pass a variable to a method multiple times without creating a new object?

For example, if I have a variable x which is the user input, another variable called m and a method were: if x is "h" then m is "example1" else if x is "f" m is "example2".

If I write:

String x = Scanner.next();

And I create the object passing the x variable, when I write,

System.out.println(obj.m);

If the input was h It will print out "example1"

But if write down this after what i showed up:

x = Scanner.next();
System.out.println(obj.m);

Whatever character I write down the output will be "example 1"

If I type "f" the first time the output will be "example2" But the second system.out.println() will print "example2" eventually if I typed "h" the second time

So is it possible to pass a variable only one time with a value that changes over time without creating a new object?

2
  • Your question does not make sense. We don't know what obj is. Post a MCVE. Commented Nov 10, 2018 at 20:32
  • can you show the logic of how "m" is assigned a value? most probably, you need to evaluate the m assignment everytime you change the value of x Commented Nov 10, 2018 at 20:37

2 Answers 2

2

If I understand your question correctly, then yes, you can pass a variable to a method multiple times without creating a new object. Let's say you create a class like this:

public class Test {
    public String m;
    
    public void testMethod(String x) {
        if ("h".equals(x)) {
            m = "example1";
        } else if ("f".equals(x)) {
            m = "example2";
        } else {
            m = "other";
        }
    }
}

If you created an object from this class in a main method and pass in different values of x as the argument for testMethod(), the value of m would change:

public class MainClass {
    public static void main(String[] args) {
        Test obj = new Test();
        
        String x = "h";
        obj.testMethod(x);
        System.out.println(obj.m); // prints example1
        
        x = "f";
        obj.testMethod(x);
        System.out.println(obj.m); // prints example2
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Instead of x.equals("h") I always use "h".equals(x), because it works even if x is null. No NullPointerException.
Thank you so much for the answer! But would a "Setter" method do tthe trick as well?
0

As I understood your question, I have added a solution that will create the object you mentioned one time and call the method inside it repeatedly as you enter values. This might help you

import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ClassOfYourObject object = new ClassOfYourObject();
        while (true) {
            System.out.print("Enter letter : ");
            String x = scanner.next();
            object.yourMethodToPrint(x);
        }
    }
}

class ClassOfYourObject {
    void yourMethodToPrint(String value) {
        if (value.equals("h")) {
            System.out.println("example1");
        } else if (value.equals("f")) {
            System.out.println("example2");
        } else {
            System.out.println("Invalid letter");
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.