Can anyone explain why this works.
I have 2 classes in Eclipse. A class called "Car" contains the following code.
public class Car { public void printOut(String variable1){ System.out.println("Hello " +variable1); } }
and another class, where my "main" one, is called the "house", the code inside it
import java.util.Scanner; class House { public static void main(String args[]){ Scanner input = new Scanner(System.in); Car carObject = new Car(); System.out.println("Enter name here: "); String variable2 = input.nextLine(); carObject.printOut(variable2); } }
When I run the code, it works, it writes "Enter the name here", and when I cross it out, it continues to say the name "Hello" "
My question is: do 'variable1' and 'variable2' have something to do with eachother, except that they both belong to the String class.
because I'm confused why the code compiles correctly.
It seems to me that variable 1 has no correlation with variable2, although they are both String classes, they are not similar to each other, and variable1 is not used in the "House" class at all, but it still knows to compile everything that I entered . As if "variable1" is being replaced by "variable2", and every variable2 contains is being printed.
Faceless void
source share