With method overloading, multiple methods can have the same name with different parameters:
int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)
Consider the following example, which has two methods that add numbers of different type:
Instead of defining two methods that should do the same thing, it is better to overload one. In the example below, we overload the plusMethod method to work for both int and double:
Note: Multiple methods can have the same name as long as the number and/or type of parameters are different.
In Java, you can only use variables in the same area where they were created. We call this "scope."
Variables that are declared directly inside a method can be used anywhere after the line of code where they were declared:
All of the code between two curly braces is called a block of code. Variables declared inside blocks of code can only be accessed by code that comes after the line where the variable was declared.
A piece of code can stand alone or be part of an if, while, or for statement. In the case of for statements, variables declared in the statement itself are also available inside the scope of the block.