I originally posted a comment. But I decided to post an answer because I believe many are misreading this question.
It looks like you are actually asking about parameters
and what they are used for, not about getters, setters, and encapsulation.
I did a few searches and came across this link:
http://www.homeandlearn.co.uk/java/java_method_parameters.html
That's a beginner's guide to Java specifically explaining what parameters are in Java.
The methods
you write in Java will sometimes need values to complete their tasks. It is likely that you won't want your method to own these variables or inherit these variables, many times you will just want to give a method a variable so it can temporarily use it for a quick task or process then return a value. That is the String name
you see in the method definition, it is a parameter, a variable that is passed into the method. You can have as many variables as you like passed into a method. The variables will be assigned in order of the way you defined the method.
For example, if you define:
public void myMethod(String one, String two, String three) {
some code;
}
And then you call that method like this:
myMethod("one", "two", "three");
Then inside myMethod, you will now be able to use the variables one
, two
, and three
to access those String values.
An example of why you would want to pass in a value instead of having the method have access to it via the class is the java Math
class. When you call Math.floor(decimalValue)
you want to give the Math class your decimalValue and have the Math class use it to determine the floor then return it to you. You don't want to write your program so that both the Math class and everything else have access to your decimalValue
all the time.
Does this explain it all?