Java Java Objects Meet Objects Methods

Andrada Terenche
Andrada Terenche
1,103 Points

When do we use parameters?

The teacher says public String getCharacterName() {return characterName } doesn't need any parameters, although we need characterName. I think maybe this is because the method is inside the class where characterName is defined, but what if we used other parameters inside the method that were defined somewhere else and also wanted to return something? Would we need parameters? Also, what's the difference regarding parameters when we use void and when we use a method that returns something?

1 Answer

Konstantinos Pedarakis
Konstantinos Pedarakis
21,301 Points

Hello Andrada Terenche im not sure if i understood quite well what you are asking. you mean when we use parameters in general? parameters are used to help us manipulated variables in a specific method, do some things and then return something back to the caller. in the example you asking the

String getCharacterName() {return characterName }

is a very special method that we use to call them as "getters". these getters and setters are sitting always in the class that the respective variables are. so for instance in this example the variable "characterName". this is a way to retrieve - "get" and "set" the variables from anywhere on the program.

on your second question regarding regarding void. voin in Java means that the method does not return anything. so at its very simple form a method that takes a parameter and is void (returning nothing), is just a method that just make a change to something(probably the variable as a parameter) and that's it. when it returns something it means that probably we will need that change in the future and maybe we want to stored it somewhere, or we need the return value to be processed by another method.

Again. im not sure if i got what you are asking, but in general i typed some of the differences that you may find usefull about variables and methods. :)