Roof why the variable does not change its value?

import java.util.Scanner;

public class House {

Scanner in = new Scanner(System.in);

int chooseRoof(int roof) {
 for( ; ; ) {
 System.out.println("Choose your roof material:");
 System.out.println("Default roof - 45 \t Tile roof - 95 \t Slate roof - 126");
 System.out.println("Enter a number of the choosed material:");
 String input = in.nextLine();

 // checking the input if input is empty, warn user about it
 // and ask to try again, after that - continue loop.

 if(input.isEmpty()) {
 System.out.println("You don't enter anything.");
System.out.println();
continue;
}

 // checking the input if input have any characters, warn user about it
 // and ask to try again, after that - continue loop.

if(!input.matches("[-+]?\\d+")){
 System.out.println("The number must consists of only numerals.");
System.out.println();
continue;
}

 if (input.equals("45") || input.equals("95") || input.equals("126")) {
 roof = Integer.parseInt(input);
 return roof;
}
 else {
 System.out.println("Your enter wrong number. Pls choose the material and "
 + "use his number.");
System.out.println();
continue;
}
}

}


public class Building {
 public static void main(String args[]) {
 int roof = 45;
 House home = new House();
 home.chooseRoof(roof); // Variable roof needs to change their value
 System.out.println(roof); // But when we print, we see the old value
 // why? How to fix it?
}
}
June 7th 19 at 14:53
1 answer
June 7th 19 at 14:55
Solution
In java primitive types are passed? By value or by reference? By value, of course. This means that changing the value of this variable in the method chooseRoof() will not affect the value of the variable in the main () method.
Non-primitive types are passed by reference, and, nevertheless, the change in the method parameter does not affect the value of the variable in the calling method. - Art_Christians commented on June 7th 19 at 14:58
You want to say that:
class A { // What is not a non-primitive type?
 public int x;
}

class B {
 public static void main() {
 A A = new A();
 a.x = 3;
change(a);
 System.out.println(a.x); // Prints 3, in your opinion?
}

 public static void change(A A) {
 a.x = 5;
}
}

Under the "non-primitive" do you understand wrapper classes Integer, Double and so on? - Manuela.Ho commented on June 7th 19 at 15:01
you misrepresented the example. The correct analogy was
public static void change(A A) {
 a = new A(5);
 }
- Art_Christians commented on June 7th 19 at 15:04
You, in this case generally assigned to the variable a reference to another object. Why the variable in the calling method needs to change its value? - Manuela.Ho commented on June 7th 19 at 15:07
and I think - why? And you're arguing the impossibility of changing a variable in the calling method that is passed primitive, and that means value.
In java primitive types are passed? By value or by reference? By value, of course. And this means that changing the value of this variable in the method chooseRoof() will not affect the value of the variable in the method main().
- Art_Christians commented on June 7th 19 at 15:10
,
And you're arguing the impossibility of changing a variable in the calling method that is passed primitive, and that means value.

And? It is not the truth? Primitives are passed by value. The value is copied to the called method. If passed a reference type (non-primitive), then the method is passed a reference to the object.

P. S are we friends with you or something? Long "you" went? - Manuela.Ho commented on June 7th 19 at 15:13
,
the method is passed a reference to the object.

Keep reasoning, feel free. - Art_Christians commented on June 7th 19 at 15:16

Find more questions by tags Java