Welcome to ICSE Java Answers. Ask any questions you have related to Java and we will answer them.
Java is a registered trademark of Oracle. This site is in no way related to or endorsed by Oracle.

What is Static Member Method?

0 votes
asked by Shashank (48 points)

2 Answers

0 votes

A static member method can be involved directly using the class name. There is no need to create an object of the class and invoke the method on that object.

The keyword 'static' is used to define a static method.

public static void method()

Non static methods cannot be invoked using the class name. They can be invoked only on an object of the class in which they are defined.

Example

The Example class defined below contains two methods - meth1 which is static and meth2 which is non-static.

public class Example {

    public static void meth1() {
        System.out.println("Method 1");
    }

    public void meth2() {
        System.out.println("Method 2");
    }
}

public class ExampleTest {
 
    public static void main(String[] args) {
        // As meth1 is static, it can be invoked using the class name
        Example.meth1();
        // meth2() is non static, so the following line would give a compilation error
        // Example.meth2();
        // To invoke meth2(), we can to create an object
        Example ex = new Example();
        ex.meth2();
        // static methods can be invoked on an object also
        ex.meth1();
    }
}

Output

Method 1
Method 2
Method 1

Related - http://www.icsejavatutorial.com/answers/484/what-is-a-static-variable

answered by Ranjith (2,048 points)
0 votes
We could also say a method which cannot be instantiated.
answered by Soham Thakurata (333 points)
...