Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods with Parameters in Java

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.

like image 302
Faceless Void Avatar asked Oct 21 '12 07:10

Faceless Void


People also ask

What are method parameters in Java?

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.

Can you use methods as parameters in Java?

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.

Can methods have parameters?

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.

How do you call a parameter method in Java?

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.


2 Answers

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

like image 149
imulsion Avatar answered Nov 03 '22 05:11

imulsion


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.

like image 26
Matthew Kennedy Avatar answered Nov 03 '22 06:11

Matthew Kennedy