METHOD OR FUNCTION IN JAVA
Java support 6 types of method
- Static method
- Non-static method
- Final method
- Abstract method
- Native method
- 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
- A method declared with the static keyword is known as static method.
- 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
- Final variable
- Final method
- 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
- If the method is declared with an abstract keyword is known as an abstract method.
- If the method is abstract it cannot have a body or block.
- Method without in body.
- If any method of a class is abstract then the corresponding class is declared as an abstract keyword.
- The abstract class may or may not contain an abstract method.
- If the class s abstract it cannot be instantiated(we cannot create the object).
- 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
21 replies on “Method or Function in Java”
I got what you mean,saved to fav, very decent site. I have been checking out many of your stories and i can state pretty
good stuff. I will make sure to bookmark your blog.
Thank U
I got what you mean,saved to fav, very decent site. I have been checking out many of your stories and i can state pretty
Thnq
Thnq for giving the feedback
This is a impressive story. Thanks!
Thanks for giving the feedback…
With thanks! Valuable information!
Thanks for giving the feedback
With thanks! Valuable information!
With thanks! Valuable information!
Thanks for giving the feedback
With thanks! Valuable information!
Thanks for giving the feedback
I have been checking out many of your stories and i can state pretty
Thanks for giving the feedback
With thanks! Valuable information!
thnx for giving the feedback
I conceive this web site has got some real good information for everyone
Thanks for giving the feedback