Functions in Java

Introduction: This tutorial is on how to create and use methods in Java. What's a Method: A method is similar to what you may call a function in other languages. Well, actually, it's exactly the same. A function, or a method, is a script which can be called from other scripts in order to run some code, and return a value. The only difference between a function and a method, is that methods are the name given to functions in an Object Oriented Programming (OOP) language, such as Java. OOP languages allow multiple running instances of classes as objects which is why they are good for creating applications such as games where multiple characters would be in use at any one time. Basic Java Template: Here is the basic Java template for a file which will, when executed, output a simple string line of 'Hello World!' to the console...
  1. public class Functions {
  2. public static void main(String args[]) {
  3. System.out.println("Hello World!");
  4. }
  5. }
So what we have here, is a public class named 'Functions', and the main method. The main is much like any other method you can create and modify to your own needs for your application, except it is automatically invoked/ran once the program is executed from the host Operating System (OS). It accepts one parameter named 'args' which is a string array, this is filled with additional arguments for if you run your application file from a Command Prompt (CMD) on Windows (Terminal on Linux) with switches, like so; -Args empty- java myJavaFile.jar -With Args- java myJavaFile.jar Argument1 arg2 allMyArgumentsAreHere Custom Function: Now we are going to create a custom function which is going to contain our 'Hello World!' output message line from our 'main' method it is currently sitting in. To create a custom method, we create it like so; Publicity (Public/Private/Protected) (static) (final) returnType functionName(Parameters) So to create a private (only the creating class can use it) static function named 'output' with a return type of 'void', it would look like this...
  1. private static void output() {
  2. System.out.println("Hello World!");
  3. }
This function does not take any additional parameters, and it simply outputs the 'Hello World!' string to our console just like before. Calling: There's just one problem with our new script;
  1. public class Functions {
  2. public static void main(String args[]) {
  3. }
  4.  
  5. private static void output() {
  6. System.out.println("Hello World!");
  7. }
  8. }
If we run it, we don't get any output, and the program terminates itself automatically. This is because the only function being ran currently is the automatically invoked 'main' method. The main method then has nothing to do, and so the program terminates itself. To make the 'main' method invoke our 'output' method, we need to call it. Calling a function is easy. We simply type the name of a function, followed by any parameters required in brackets and separated by a comma. But since our 'output' function doesn't take any parameters (we can see this by the empty brackers, '()') we simply type the function name followed by empty brackets. Like so...
  1. output();
So our new and improved class looks like this;
  1. public class Functions {
  2. public static void main(String args[]) {
  3. output();
  4. }
  5.  
  6. private static void output() {
  7. System.out.println("Hello World!");
  8. }
  9. }
including our new function call to the 'output' function. Finished! The output we receive from both our first and last editions of our function class is; 'Hello World!'

Add new comment