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

Anas Rida
Anas Rida
8,183 Points

when to use brackets or curly braces

when we created the mCharacterName object, we defined it by writing = "Yoda". When we created the getCharacterName object, we opened curly braces and inside them wrote the code to return the mCharacterName. I am not grasping when to use and when not to use the curly braces. Can someone please clarify this?? Many thanks

3 Answers

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

{} is used to enclose a group of code that gets associated together and () is used to enclose function arguments

Anas,

Lets break it up into the common brackets used in Java

  • [ ] : this is normal used when declaring scope of an array

    int x [10];  
    
  • { } : defines a block of code. You could also nest the block of code

 { // Begin outer scope
     if(x==5)
    { // Begin inner scope
       System.out.println("Hello");
    } // End one inner block
} // End of outer block
  • () : is used to contain the parameters or arguments
void Hello (int x)

So to sum it up { } defines an entire block of code; () defines the parameters or arguments for a function or when calling a function; [] is used for declaring an array

Hope that cleared your doubt to some extent

Anas Rida
Anas Rida
8,183 Points

thank you very much for explaining this!!!

Micheal Ammirati
Micheal Ammirati
5,377 Points

OK, the line: String mCharacterName = "Yoda";

Is actually called a field of the class pez dispenser. All you are doing in that line is declaring a String and assigning it the value "Yoda"

The other lines of code are called a method. You are not creating an object there. You can think of it as creating some function that the class can perform. That method must return a String since you used the String keyword in the declaration. Since methods can perform a lot of different things you need to enclose all of that code inside of curly brackets. The parenthesis you see before the curly braces are where you would take in parameters to your method. Since the getCharacterName method does not take in any parameters the parenthesis are empty. All methods use those parenthesis, both when you declare them and when you use them somewhere else in your code. Even if the method has no parameters.

Anas Rida
Anas Rida
8,183 Points

Yes I understand now. Thank you very much for making this point clearer!!!!