Skip to content
Code To Live By » Java Tutorial » Functions in Java

Functions in Java

  • by

Functions, also referred to as Methods, provide us with a way that we can separate and reuse code, rather than having the entirety of our code contained within a “main” method.

Speaking of the “main” method, you’ve already seen a function before! The “main” method that you can generate for a created class in Eclipse is an example of a function.

public static void main(String[] args) {
    [...]
}

Let’s start by breaking down some of the structure of the main method so that we know what we are looking at.

Access Modifier

An “access modifier” dictates who is allowed to call a function. The following access modifiers are available in Java:

  • public – The method is available to anyone.
  • private – The method may only be used inside the current class.
  • package (default) – The method is available to other classes in the same package.
  • protected – The method is available to other classes in the same package, and also to subclasses of the current class.

In our main method we see that our access modifier is “public.”

Other Modifiers

After the access modifier we have also have the ability to add other modifiers.

In our main method we see that we also have a “static” modifier, meaning that our main method can be called independently of its class, whereas non-static methods, or “instance” methods, need an “instance” of their class.

There are a couple other modifiers in this vein, but we will leave them for future posts.

Return Type

After all the modifiers we will define the “return type” for our function. In many cases we will want to return some object from our function to be used by the caller. In order to do that we need to know what data type would be returned, so this is where we would define this.

Not all functions will return values though, for example in our main method we have a return type of “void” which means that the main method does not return anything to its caller.

Name

This is, as you guessed it, the name of the function / method. Function names follow the same rules as Variable names that we have discussed in the past.

The name of the main method is of course “main” in our example.

Parameters

Parameters provide us with a way to pass in information to our functions. These can essentially be thought of as variables within the scope of the function. Like variables, parameters will have a data type and a name and follow the same rules.

In our main method we see that we are receiving a String array with the name args.

These are command line arguments that can be passed into the main method of your program. I won’t go much into detail on this because frankly its not something I find myself using often in Object Oriented Programming, but if you plan to do a lot of scripting it might be more valuable. You can consult Oracle’s documentation on command line arguments if you wish to read more.

Body

Now that we have gone through the declaration of the main method, we would define the body of the method. This would be the actual code that our function / method would be executing.

Another Example

Let’s return to some of our well trodden example code, saying “Hello” to things.

If we think back to our Input / Output post we had some code to read in a name from a Scanner

System.out.println("What is your name?");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
scanner.close();
System.out.println("Hello " + name + "!");

We could clean this up slightly by abstracting away a lot of the logic that is needed for scanners, and then we can deal with only the name. If we were to rewrite this as a function we may end up with something like this

public static void main(String[] args) {
	String name = readNameFromScanner();
	System.out.println("Hello, " + name + "!");
}

public static String readNameFromScanner() {
	System.out.println("Please enter your name:");
	Scanner scanner = new Scanner(System.in);
	String nameFromScanner = scanner.nextLine();
	scanner.close();
	return nameFromScanner;
}

Our method named readNameFromScanner is public and static, like our main method, but it is has a return type of String as we will be returning the name that we read from the scanner. Our method also does not take any parameters. Calling our new method from main is as simple as referencing our method name and storing the return value.

Now we can keep the “how” of reading this name from a scanner in its own separate function, where we can reuse it if we need to read another name later on. As a result, our main method is much more clear as it can focus on simply what we wish to do with the name afterwards, in our case to say hello.

Now you have a good idea of how to create and use functions and methods in Java!

Leave a Reply

Your email address will not be published. Required fields are marked *