-
Notifications
You must be signed in to change notification settings - Fork 1
Methods
This week we're introducing methods. We'll go over the different parts of the main method as well as how to write your own methods to use.
- public static void main(String[] args)
- public vs private
- static
- void, int, double, String
- return statement
- Arguments and Parameters
- Local vs Global Scope
Let's break down every part of that statement
This statement basically means that you have a method called main
that:
- is accessible anywhere and by anyone (
public
) - you can call
main
without having an object present (static
) - has no return value (
void
) - can contain String based arguments (
String[] args
)
These are the basic parts of a method.
Methods can either be public, private, or in some cases protected. We'll go over protected when we begin talking about classes/objects later on.
- public - can be accessed directly inside or outside a class
- private - can only be accessed within the class
We'll go over public and private methods more in depth when we start talking about classes. For our current purposes, public is good enough.
Static is an optional keyword that can be applied so that you could use a method without having to declare an object of the class containing the method.
> public class Example {
> public int add(int a, int b) {
> return a + b;
> }
>
> public static sub(int a, int b) {
> return a - b;
> }
>
> public static void main(String[] args) {
> int result = Example.sub(5, 10); // is ok because sub is static
> System.out.println(result);
> result = Example.add(5, 10); // throws an error because an Example object was not declared
> System.out.println(result);
> }
> }
/Example.java:13: error: non-static method add(int,int) cannot be referenced from a static context
result = Example.add(5, 10); // throws an error
^
1 error
The correct way to run the add
method from above would've been to write the code like so:
> ...
> Example test = new Example();
> result = test.add(5, 10);
> ...
This declares an object named test
of the class Example
. Remember that the new
keyword instantiates (creates) an object in memory with the attributes of the Example
class. As a result, you now have access to the nonstatic methods of Example
.
A method must also have a return type listed. All the methods you've seen so far have been of the type void
meaning that they don't return anything. If your method returns something though you must specify the type returned and also use the return
keyword at the end of the method.
It should also be noted that a method can only have one return type.
> public class Example {
> public static int add(int a, int b) {
> int c = a + b;
> return c;
> }
>
> public static void main(String[] args) {
> System.out.println(Example.add(5, 6);
> }
> }
11
In the example above, we have a method takes two integer numbers and returns their integer sum.
So we declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value.
> public static int add(int a, int b) {
> int c = a + b;
> return c; // this function add will return the value stored in c
> }
> public static int add(int a, int b) {
> int c = a + b;
> return a + b; // this function will return the same value
> }
The data type of the return value must match the method's declared return type.
If you try to return a value from a method that is declared void, you will get a compiler error. i.e. - you can't return an integer value from a method declared to return a boolean.
> public static int add(int a, int b) {
> return false; // this function is invalid ; add needs to return an integer
> }
We can pass values to our methods so that actions can be done with this value. When we defined our functions, we use parameters to reference those values.
> //this function requires two parameters as inputs; a and b
> public static int add(int a, int b) {
> return a + b;
> }
Lets say we now want to use this 'add' method to use in our program. Lets say we want to add the price of two fruits,'apple' and 'orange', and store the value in a variable called 'total'.
Do do so, we would call the method 'add' in our code, and pass the values of the variables as arguments.Arguments are the actual values of variables that gets passed to function.
> ....
> int apple = 1; //an apple cost $1
> int orange = 3; //an apple cost $1
> ...
> int total = add(apple, orange);
> ...
> public static int add(int a, int b) {
> return a + b;
> }
In lamest terms, we are passing the values of the variables into the 'add' function. The method returns the sum of both inputs, and the main program stores that value in a new variable, 'total'.
Similar to how an object stores its state in fields, if needed, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field.
> public static int add(int a, int b) {
> int c = a + b;
> return c; // the int variable c is a local variable to the method add
> }
There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared, but we will expand on that at a later time.
We can think of it as the int variable c only exists within the method; because it was defined within a function, it cannot be accessed outside of the function 'add'.
> public class Main{
> //Because these variables were defined within the 'Main' class, any method defined within this class can access them
> int apples = 1;
> int orange = 3;
>
> public static int add(){
> int total = apples + orange;
> return total;
> // the variable total was only defined within the add method; it is only accessible within this function
> }
> }
Global variable is a term used to refer a variable which can be accessed globally within a class. In this example, the variables apples and orange were global variables.