Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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. :)