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);
}
}
CodePudding user response:
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);
}
}
CodePudding user response:
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.