Static Methods Java Example
1. Introduction
In this article, we will take a look at Java Static Methods. These are used for computational tasks or stateless functions. Static methods can be defined in java classes and interfaces.
2. Java Example
These are not dependent on class instances. They do not operate on instance properties. They perform operations from the parameters of the method. They do not have references to class variables.
2.1 Prerequisites
Java 8 is required on the Linux, Windows or Mac operating system. Eclipse Oxygen can be used for this example.
2.2 Download
You can download Java 8 from the Oracle web site . Eclipse Oxygen can be downloaded from the eclipse web site.
2.3 Setup
2.3.1 Java Setup
Below are the setup commands required for the Java Environment.
Setup
JAVA_HOME="/desktop/jdk1.8.0_73" export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH
2.4 IDE
2.4.1 Eclipse Oxygen Setup
The ‘eclipse-java-oxygen-2-macosx-cocoa-x86_64.tar’ can be downloaded from the eclipse website. The tar file is opened by double click. The tar file is unzipped by using the archive utility. After unzipping, you will find the eclipse icon in the folder. You can move the eclipse icon from the folder to applications by dragging the icon.
2.4.2 Launching IDE
Eclipse has features related to language support, customization, and extension. You can click on the eclipse icon to launch eclipse. The eclipse screen pops up as shown in the screenshot below:
You can select the workspace from the screen which pops up. The attached image shows how it can be selected.
You can see the eclipse workbench on the screen. The attached screenshot shows the Eclipse project screen.
Java Hello World
class prints the greetings. The screenshot below is added to show the class and execution on the eclipse.
2.5 Characteristics
A static method in java has multiple arguments and returns a value. A java class has methods that are of static and instance types. Furthermore, they can be overloaded by varying the signatures of the method. Typically return keyword is used in static methods to return a value from the method. A java static method can return a single value as specified in the java method signature. Static methods can operate on the parameter variables and static variables mentioned in the class. An interface can have static methods defined and default implementation can be provided.
2.6 Static methods over Instance
Instance methods of a class are very specific to a class instance. A class needs to be instantiated to be invoked using the instantiated object. The instantiated object will have a specific method of the object’s class. The instance methods can be overridden and dynamically bound at runtime.
These are used when can be at the class level instead of the instantiated object. A class can have overloaded static methods but no overridden static methods. A static method is bound using static binding.
Static methods are chosen over Instance methods for computational tasks where there is no state maintained. Data Transformations and utility functions are examples where they are chosen over instance methods. In the case of Entity modeling, each entity has a state where instance methods are used to modify the state of the entity
2.7 Example
StaticMethodExample
Class example is shown in the code below:
class StaticMethodExample{ public static int sum = 0; public static void getSum(int a, int b) { sum = a + b; } } public class Runner { public static void main (String[] args) { StaticMethodExample.getSum(1,2); System.out.println(StaticMethodExample.sum); StaticMethodExample example = new StaticMethodExample(); example.getSum(1,2); System.out.println(example.sum); } }
The code above when executed will output:
Output
3 3
Let us now look at the interface.
interface InterfaceExample { static int getProduct(int a, int b) { return a*b; } } public class ExampleRunner { public static void main(String[] args) { int product = InterfaceExample.getProduct(3,4); System.out.println(product); } }
The code above when executed will output as shown below.
12
3. Download the Source Code
You can download the full source code of this example here: Static Methods Java Example