Could someone 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 , which is where my 'main' is, is called "House", the code inside it is
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 name here" and when I type it out, it proceeds to say "Hello "name entered" "
My question is, do 'variable1' and 'variable2' have any relation to eachother, other than that they're both of String class.
because i'm confused as to why the code compiles correctly.
To me, it looks like variable 1 has no correlation to variable2, even though they're both of String class, it doesn't look like they ever interact with one another, and variable1 isn't used in the "House" class at all, yet it still knows to compile whatever I've entered. It's as if 'variable1' is replaced by 'variable2' and whatever variable2 contains gets printed out.
Parameters and Arguments Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
There's no concept of a passing method as a parameter in Java from scratch. However, we can achieve this by using the lambda function and method reference in Java 8.
The parameters are used in the method body and at runtime will take on the values of the arguments that are passed in. Note: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked.
There are two ways to call a method with parameters in java: Passing parameters of primtive data type and Passing parameters of reference data type. 4. in Java, Everything is passed by value whether it is reference data type or primitive data type.
The method definition in class Car
is sort of a prototype for when you use it. Have you ever been taught functions in maths with a 'black box'? You put in a number, and get output. So, you enter 3, if the function is f(x) = Xx2, the output will be 6. Before you call the method, var2 is completely different from var1. In the method however, var2 is passed and replaces all var1s you use in the method. Don't worry, I didn't get this either when I started Java
They're related only because you are passing variable2
into your Car.printOut
method. Imagine this analogy for a moment: You know how to do math homework on command. You are an object, called FacelessVoid
and you have a method called doHomework
. doHomework
takes a single parameter of type Work
. In the real world, I would have a box of work, and I would dump the box of work onto your desk. Whatever I write on the box doesn't matter to you, but the contents of the box get dumped onto your desk, where you can call it whatever you want.
This is exactly what is happening in your code: You have a string called variable2
, and its contents get "dumped" into variable1
inside printOut
. Of note though is that the string doesn't actually get dumped
into your method, a reference gets copied. So it's the same object, it's just called something different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With