How to Get Main output into Common() method in Java?

Swap() method in java , why this output?

  • I have this simple code and i would like to swap the variables a and b but the output is the same as in the beginning and i want to know why? my code public class Main { static ...show more

  • Answer:

    You don't need a temp variable: public class Main { static String a; static String b; public static void main(String[] args) { a = "First String"; b = "Second String"; swap(a, b); System.out.println("The strings after swapping are "+a+" "+b); } public static void swap(String first, String second) { a = second; b = first; } }

C7VCA2IUEDH6HW472FFCXPRIYE at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

The changes are local. You can do this public class Main { static String temp; static String a1; static String b1; public Main(){ a1 ="First String"; b1 = "Second String"; swap(a1,b1); System.out.println("The strings after swapping are "+a1+" "+b1); } public static void main(String[] args ){ new Main(); } private static void swap(String a, String b){ a1 = b; b1 =a; System.out.println(temp); } }

sope

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.