Kamis, 21 Agustus 2014

Difference between static vs non static method in Java



In this article, we will take a look at difference between static and non static method in Java, one of the frequently asked doubt from Java beginner. In fact, understanding static keyword itself is one of the main programming fundamental, thankfully it's well defined in Java programming language . A static method in Java belongs to class, which means you can call that method by using class name e.g. Arrays.equals(), you don't need to create any object to access these method, which is what you need to do to access non static method of a class. Static method are treated differently by compiler and JVM than non static methods, static methods are bonded during compile time, as opposed to binding of non static method, which happens at runtime. Similarly you can not access non static members inside static context, which means you can not use non static variables inside static methods, you can not call non static methods from static ones, all those will result in compile time error. Apart from these significant differences between static and non static methods, we will also take a look at few more points in this Java tutorial, with a code example, which shows some of these differences in action.By the way this is the second article on static method, in first article, we have learned about when to use static method in Java








Static vs Non Static method in Java



Let's see couple of differences between static and non static method in Java programming language, which is enforced by language itself.



What is difference between static and non static method in Java1) I think first and foremost difference between them is that you can call static method without creating any object e.g. Collections.sort(). This makes static method useful utility,  while you need to instantiate an object to call non static method in Java. Static methods are also pretty useful on several design pattern including Factory and Singleton.



2) You can not access a non static variable inside any static method in Java, but reverse is fine i.e. you can access static variables or call static method from a non static method without any compile time error.



3) One more worth noting difference between static and non static method is that you can not override static method in Java. They are bonded during compile time using static binding. Though you can create a similar static method in sub class, that is know as method hiding in Java.



4) static methods are known as class methods, if you synchronize static method in Java then they get locked using different monitor than non static methods e.g. both static and non static methods are locked using different monitor in Java, and that's why its grave Java mistake to share a resource between static and non static method in Java.



5) Static methods are commonly used inside utility classes e.g. java.util.Collections or java.util.Arrays, because they are easier to access, as they  don't need an object. One of the popular example of static method is though main method, which act as entry point for Java programs.



Here is the Java program to highlight couple of differences between static and non static methods in Java :





import org.slf4j.Logger;


import org.slf4j.LoggerFactory;





/**


  * Java program to show some differences
between static and non static methods in Java.


  *


  * @author http://javarevisited.blogspot.com


  */


public class StaticMethodTest
{


 


    private
static int
version;


    private
String name;





    private
static final
Logger logger = LoggerFactory.getLogger(StaticMethodTest.class);





    public
static void
main(String args[]) {


    


        // You can
call static method directly, wihtout creating any object


        StaticMethodTest.staticMethod();


     


        // You need
an object to call non static mehtod in Java


        StaticMethodTest myObject = new StaticMethodTest();


        myObject.nonStaticMethod();


   


    }


 





    public
void nonStaticMethod(){


        logger.info("I am a non static method in
Java"
);


     


        // You can
access static variable inside non static method


        logger.info("static version from non static
method "
+ version);


    }


 


 


    public
static void
staticMethod(){


        logger.info("I am a static method in Java,
version : "
+ version);


     


        //System.out.println(name);
// compile time error


    }


}





Output:


2013-07-01
04:10:08,029
0   
[main] INFO  StaticMethodTest  - I am a static
method in Java, version : 0


2013-07-01
04:10:08,060
31  
[main] INFO  StaticMethodTest  - I am a non static
method in Java


2013-07-01
04:10:08,060
31  
[main] INFO  StaticMethodTest  - static
version from non static method 0






That's all on difference between static and non static methods in Java. You can see that non static members are not accessible inside static context and you also need to create an object, before calling a static method, due to this reason static methods are more suited as utility method e.g. Arrays.deepEquals().






























Source:http://javarevisited.blogspot.com/2013/11/difference-between-static-vs-non-static-method-java.html

Tidak ada komentar:

Posting Komentar