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

What are parameters for?

in java objects we declared the method like public void myMethod( " //parameters go here " ){ } can someone tell me what are parameters for?

2 Answers

What is passed "to" a method is referred to as an "argument". The "type" of data that a method can receive is referred to as a "parameter".

Parameters will be used in the method as a local (declared inside a method) variable would.

Umm can you elucidate a bit?

Basically they are used if you want to pass a value to a method and do something with it.

Let's say you have a method which calculates the area of a square.

if you want to be able to calculate the area of a square with different values for a side you will pass a parameter as the value of the side:

publice int getArea(int side) {
int area = side*side;
return area;
}

int b = getArea(2);

The code above will set the value of b to 4.

If you do not go with a parameter you will not be able to change the value passed as a side to the method.

Hope that clarifies things.