Java: passing variable one time but different values












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?










share|improve this question
























  • Your question does not make sense. We don't know what obj is. Post a MCVE.
    – Elliott Frisch
    Nov 10 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
    – dubes
    Nov 10 at 20:37
















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?










share|improve this question
























  • Your question does not make sense. We don't know what obj is. Post a MCVE.
    – Elliott Frisch
    Nov 10 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
    – dubes
    Nov 10 at 20:37














0












0








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?










share|improve this question















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?







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 20:34









Pushpesh Kumar Rajwanshi

4,8141826




4,8141826










asked Nov 10 at 20:30









Akk

35




35












  • Your question does not make sense. We don't know what obj is. Post a MCVE.
    – Elliott Frisch
    Nov 10 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
    – dubes
    Nov 10 at 20:37


















  • Your question does not make sense. We don't know what obj is. Post a MCVE.
    – Elliott Frisch
    Nov 10 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
    – dubes
    Nov 10 at 20:37
















Your question does not make sense. We don't know what obj is. Post a MCVE.
– Elliott Frisch
Nov 10 at 20:32




Your question does not make sense. We don't know what obj is. Post a MCVE.
– Elliott Frisch
Nov 10 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
– dubes
Nov 10 at 20:37




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
– dubes
Nov 10 at 20:37












2 Answers
2






active

oldest

votes


















1














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 (x.equals("h")) {
m = "example1";
} else if (x.equals("f")) {
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
}
}





share|improve this answer



















  • 1




    Instead of x.equals("h") I always use "h".equals(x), because it works even if x is null. No NullPointerException.
    – Francisco Delmar Kurpiel
    Nov 10 at 22:52










  • Thank you so much for the answer! But would a "Setter" method do tthe trick as well?
    – Akk
    Nov 21 at 6:55





















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");
}
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243137%2fjava-passing-variable-one-time-but-different-values%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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 (x.equals("h")) {
    m = "example1";
    } else if (x.equals("f")) {
    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
    }
    }





    share|improve this answer



















    • 1




      Instead of x.equals("h") I always use "h".equals(x), because it works even if x is null. No NullPointerException.
      – Francisco Delmar Kurpiel
      Nov 10 at 22:52










    • Thank you so much for the answer! But would a "Setter" method do tthe trick as well?
      – Akk
      Nov 21 at 6:55


















    1














    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 (x.equals("h")) {
    m = "example1";
    } else if (x.equals("f")) {
    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
    }
    }





    share|improve this answer



















    • 1




      Instead of x.equals("h") I always use "h".equals(x), because it works even if x is null. No NullPointerException.
      – Francisco Delmar Kurpiel
      Nov 10 at 22:52










    • Thank you so much for the answer! But would a "Setter" method do tthe trick as well?
      – Akk
      Nov 21 at 6:55
















    1












    1








    1






    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 (x.equals("h")) {
    m = "example1";
    } else if (x.equals("f")) {
    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
    }
    }





    share|improve this answer














    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 (x.equals("h")) {
    m = "example1";
    } else if (x.equals("f")) {
    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
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 10 at 22:40

























    answered Nov 10 at 20:52









    Sahil Makhijani

    538




    538








    • 1




      Instead of x.equals("h") I always use "h".equals(x), because it works even if x is null. No NullPointerException.
      – Francisco Delmar Kurpiel
      Nov 10 at 22:52










    • Thank you so much for the answer! But would a "Setter" method do tthe trick as well?
      – Akk
      Nov 21 at 6:55
















    • 1




      Instead of x.equals("h") I always use "h".equals(x), because it works even if x is null. No NullPointerException.
      – Francisco Delmar Kurpiel
      Nov 10 at 22:52










    • Thank you so much for the answer! But would a "Setter" method do tthe trick as well?
      – Akk
      Nov 21 at 6:55










    1




    1




    Instead of x.equals("h") I always use "h".equals(x), because it works even if x is null. No NullPointerException.
    – Francisco Delmar Kurpiel
    Nov 10 at 22:52




    Instead of x.equals("h") I always use "h".equals(x), because it works even if x is null. No NullPointerException.
    – Francisco Delmar Kurpiel
    Nov 10 at 22:52












    Thank you so much for the answer! But would a "Setter" method do tthe trick as well?
    – Akk
    Nov 21 at 6:55






    Thank you so much for the answer! But would a "Setter" method do tthe trick as well?
    – Akk
    Nov 21 at 6:55















    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");
    }
    }
    }





    share|improve this answer


























      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");
      }
      }
      }





      share|improve this answer
























        0












        0








        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");
        }
        }
        }





        share|improve this answer












        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");
        }
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 20:52









        Sand

        752112




        752112






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243137%2fjava-passing-variable-one-time-but-different-values%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()