Home » Core Java » Static Methods Java Example

About Bhagvan Kommadi

Avatar photo
Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.

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:

Static Methods Java - Launching IDE
Launching IDE

You can select the workspace from the screen which pops up. The attached image shows how it can be selected.

Static Methods Java - IntelliJ vs Eclipse
Eclipse Workspace

You can see the eclipse workbench on the screen. The attached screenshot shows the Eclipse project screen.

Static Methods Java - Eclipse Workbench
Eclipse Workbench

Java Hello World class prints the greetings. The screenshot below is added to show the class and execution on the eclipse.

Static Methods Java - Java Hello
Java Hello

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

Download
You can download the full source code of this example here: Static Methods Java Example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments