Java – Static Methods

Method:

  • A block of instructions having identity.
  • Methods takes input(parameters), process input and return output.
  • Methods are used to perform operations on data
SyntaxExample
returntype identity(arguments)
{
            statements;
}
int add(int a, int b)
{
            int c=a+b;
            return c;
}

Classification of Methods: Based on taking input and returning output, methods are classified into 4 types.

Method Definition: Method definition consists logic to perform the task. It is a block.

Method Call: Method Call is used to invoke the method logic. It is a single statement.

No arguments and No return values method:

class Code
{
            public static void main(String[] args) {
                        Code.fun();
            }
            static void fun(){
                        System.out.println(“fun”);
            }
}

With arguments and No return values:

class Code
{
            static void main(String[] args) {
                        Code.isEven(4);
                        Code.isEven(13);
            }
            static void isEven(int n){
                        if(n%2==0)
                                    S.o.p(n+” is Even”);
                        else
                                    S.o.p(n+” is Not even”);
            }
}

With arguments with return values:

class Code
{
            static void main(String[] args) {
                        int r1 = Code.add(10, 20);
                        S.o.p(r1);
            }
            static int add(int a, int b){
                        return a+b;
            }
}

No arguments and with return values:

class Code
{
            static void main(String[] args) {
                        double PI = Code.getPI();
                        S.o.p(“PI val : ” + PI);
            }
            static double getPI(){
                        double PI = 3.142;
                        return PI;
            }
}
Scroll to Top