Method or Function in Java

java method

METHOD OR FUNCTION IN JAVA

Java support 6 types of method

  1. Static method
  2. Non-static method
  3. Final method
  4. Abstract method
  5. Native method
  6. Synchronized method

Static Method

  • The method declared with the static keyword is known as a static method.
  • If the method is static we can call it by class name, directly and object name.
  • The static method can access static data member and can change the value of it.

PROGRAM

class Test

{

                int x=10;

                static void show()

                {

                                System.out.println("I am in show method");

                }

                public static void main(String[] args) 

                {

                                show();                //call directly

                                Test.show();      //call by class name

                                Test t=new Test();           //object creation

                                t.show();             // call by object name

                }

}

 

 

O/P I am in show method

I am in show method

I am in show method

 

 Non-static method

  1. A method declared with the static keyword is known as static method.
  2. If the method is non-static then it is only called by object name.

PROGRAM

class Test

{

                int x=10;

                 void show()

                {

                                System.out.println("I am in show method");

                }

                public static void main(String[] args) 

                {

                                Test t=new Test();           //object creation

                                t.show();             // call by object name

                }

}

O/P I am in show method

 Final Method

Final is a keyword in java which can be used in 3 different places

  1. Final variable
  2. Final method
  3. Final Class

If the method declared with the final keyword is known as the final method.

If the method is final it cannot override[if the signature of superclass method is totally the same as the signature of the subclass method is known as method override].

The final method can be declared as the method declaration because it indicates the method cannot be overridden by the subclass.

PROGRAM

class Test

{

                final void show()

                {

                                System.out.println("i am in show  Function");

                }

                public static void main(String[] args) {

                                new Test().show();

                }

}

O/P i am in show  Function

Native method

The method declared with the native keyword is known as the native method in java

The native method is used to communicate java languages to other environments like C or C++.

The native method also indicates that method existence shouldn’t be checked at the time of compilation.

Abstract Method

  1. If the method is declared with an abstract keyword is known as an abstract method.
  2. If the method is abstract it cannot have a body or block.
  3. Method without in body.
  4. If any method of a class is abstract then the corresponding class is declared as an abstract keyword.
  5. The abstract class may or may not contain an abstract method.
  6. If the class s abstract it cannot be instantiated(we cannot create the object).
  7. The abstract method is used to declare a prototype in java.

PROGRAM

abstract class Test

{

                                abstract void show();

                                abstract void fun1();

                                int x=90;

                void display()

                {

                                System.out.println("display()....");

                }

}

abstract class Demo extends Test

{

                void show()

                {

                                System.out.println("show()....");

                }

}

class MyClass extends Demo

{

                void fun1()

                {

                                System.out.println("fun1()....");

                }

                                public static void main(String[] args) 

                {

                                MyClass mc=new MyClass();

                                mc.show();

                                mc.fun1();

                                System.out.println(mc.x);

                                mc.display();

                }

}

O/P  show()....

fun1()....

90

display()....

 

Synchronization Method:-

The synchronization is used to prevent the thread interference and prevent the consistency problem.

It is basically two types:-

1)Process synchronization

2)Thread Synchronization

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community

21 thoughts on “Method or Function in Java

Leave a Reply

shares