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 (Retired) Meet Objects Privacy and Methods

What exactly does ".return" do and what do the extra pair of brackets () at the end signify?

I just finished the Privacy and Methods lesson and there I two things I do not completely understand. First is .return. From what I see, it allows you to make a String retrievable despite being private. Second is the brackets. I don't know why they are necessary (though I'm sure they are) and when to use them. Thanks.

2 Answers

Allan Clark
Allan Clark
10,810 Points

return is a keyword itself, no need for the '.'

When creating methods often times there is information, or results that you will need back after the method is run, that is where return comes into play.

I'm going to assume you meant parenthesis '()' as that is what you have in the title (brackets are '{}' ). Parens denote a method, you need them when defining a method and when calling it. Parameters will go inside the parens, though for the simple getter method in the example there is no need for parameters, hence the empty parens.

To illustrate both concepts, lets say we want to create a method that will add two integers together.

public int sumInts(int num1, int num2) {
     return num1 + num2;
}

The parens hold the parameters (the numbers passed in from the method call), then we send back the results using the return keyword. Those results may be stored in a variable.

An example method call would look like this:

int result = sumInts(492, 5);

This will save 497 in the result variable.

This may be a little ahead of you in the course, I'm sure it will become clearer as you go along. Let me know if there is anything else I can clear up.

You can write a method like this:

privacyKeyword typeOfValueReturned nameOfMethod (parameter1, ..., parameterN){
    method body
}

In this case,

public String getCharacterName(){
    return mCharacterName;
}

is declaring a public method called getCharacterName which doesn't need any parameters and will return a String stored in the mCharacterName variable.

Well, that is how I see it. I hope it helps.