0

Looking for an explanation on why and how as to when your creating a void method in the same class and want to use the parameters in other methods in main how you should go about doing that. For some reason I'm being asked to intialize my string variables to null but when I plug them into the method assign it says the my paramets that I'm passing will remain null after I call the method. Why is that? After I call the assign method why isn't Java changing the variables to whatever the user enters?

import java.util.Scanner;

public class test {
public static Scanner input = new Scanner(System.in);

public static void main(String[] args){

    String Company = null;
    String URL = null;
    String Country = null;
    int Employees = 0;

    assign(Company,URL,Country,Employees);

    System.out.print(Company, URL, Country, Employees);
}

public static void assign(String companyName, String webAddress, String country, int employees){

    System.out.print("Enter the company's name: ");
    companyName = input.nextLine();

    System.out.print("Enter the web address: ");
    webAddress = input.nextLine();

    System.out.print("Enter the country: ");
    country = input.nextLine();

    System.out.print("Enter the number of employees: ");
    employees = input.nextInt();

    System.out.print(companyName);
  }

}
  • Does this answer your question? [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – tgdavies Mar 21 '22 at 05:35
  • No not really, I've read about this. But for my question specifically or better yet what would I need to do get companyName in assign method to return that value in the parameter because I've done this with arrays and it works so am I missing something? – newEra2021 Mar 21 '22 at 05:55
  • Are you saying the only way I can do this is with object? – newEra2021 Mar 21 '22 at 06:01

2 Answers2

0

As Java is pass-by-value (Please read Is Java "pass-by-reference" or "pass-by-value"?), you need to move these variables to class level, create the object of this class, pass it to assign method & accept the input into the object to work this out, something like below code. Please use camel case for variable names.

import java.util.Scanner;

public class Test {
public static Scanner input = new Scanner(System.in);

    String company;
    String url;
    String country;
    int employees;

public static void main(String[] args){

    Test t = new Test();
    assign(t);
    System.out.println(t.company+ t.url + t.country + t.employees);
}

public static void assign(Test t){

     System.out.println("Enter the company's name: ");
     t.company = input.nextLine();
     System.out.print("Enter the web address: ");
     t.url = input.nextLine();

     System.out.print("Enter the country: ");
     t.country = input.nextLine();

     System.out.print("Enter the number of employees: ");
     t.employees = input.nextInt();

     System.out.println(t.company);
   }

 }
Sup19
  • 88
  • 10
  • Seeing it like this explained it in a way that I can easily relate to. This is exactly what I needed to see and hear. Thank you truly for taking the time in explaining this. – newEra2021 Mar 21 '22 at 06:38
0

Let's take the parameter companyName as an example. When you pass companyName to the assign method, you are just passing its value, which is null. Inside the assign method, you are changing the value of companyName locally. That would have no effect on the original value in the main method.

To answer your question in your comment, yes, you can modify any object that you pass to a method inside the method itself. You can't do that with String because it is immutable. What you're doing with the String is not modifying it, but assigning it to a different value.

The parameter companyName holds a reference to the original object inside the method. If the object was mutable, you could modify it. But once you assign it to a different value, it now points to the new value and the reference to the original object is lost.

stepanian
  • 11,373
  • 8
  • 43
  • 63