Difference Betwixt Static Vs Not Static Method Inwards Java

In this article, nosotros volition accept a expect at the difference betwixt static in addition to non-static method inwards Java, i of the oftentimes asked dubiety from Java beginner. In fact, agreement static keyword itself is i of the primary programming fundamental, thankfully it's good defined inwards Java programming linguistic communication . Influenza A virus subtype H5N1 static method inwards Java belongs to class, which agency y'all tin laissez passer notice telephone telephone that method past times using degree advert e.g. Arrays.equals(), y'all don't demand to create whatever object to access this method, which is what y'all demand to practise to access non-static method of a class. Static method is treated differently past times compiler in addition to JVM than non-static methods, static methods are bonded during compile time, every bit opposed to binding of the non static method, which happens at runtime.

Similarly you tin laissez passer notice non access non static members within static context, which agency y'all tin laissez passer notice non role non static variables within static methods, y'all tin laissez passer notice non telephone telephone non static methods from static ones, all those volition number inwards compile fourth dimension error.

Apart from these meaning differences betwixt static in addition to non static methods, nosotros volition too accept a expect at few to a greater extent than points inwards this Java tutorial, alongside a code example, which shows about of these differences inwards action.

By the way this is the mo article on static method, inwards starting fourth dimension article, nosotros receive got learned virtually when to role static method inwards Java




Static vs Non Static method inwards Java

Let's come across duet of differences betwixt static in addition to non static method inwards Java programming language, which is enforced past times linguistic communication itself.

1) I retrieve starting fourth dimension in addition to initiatory divergence betwixt them is that y'all tin laissez passer notice telephone telephone static method without creating whatever object e.g. Collections.sort(). This makes static method useful utility,  piece y'all demand to instantiate an object to telephone telephone non static method inwards Java. Static methods are too pretty useful on several pattern pattern including Factory and Singleton.

2) You tin laissez passer notice non access a non static variable within whatever static method inwards Java, but opposite is fine i.e. y'all tin laissez passer notice access static variables or telephone telephone static method from a non static method without whatever compile fourth dimension error.

3) One to a greater extent than worth noting divergence betwixt static in addition to non static method is that you tin laissez passer notice non override static method inwards Java. They are bonded during compile fourth dimension using static binding. Though y'all tin laissez passer notice create a like static method inwards sub class, that is know every bit method hiding inwards Java.

4) static methods are known every bit degree methods, if y'all synchronize static method inwards Java thus they acquire locked using unlike monitor than non static methods e.g. both static in addition to non static methods are locked using unlike monitor inwards Java, in addition to that's why its grave Java error to part a resources betwixt static in addition to non static method inwards Java.

5) Static methods are unremarkably used within utility classes e.g. java.util.Collections or java.util.Arrays, because they are easier to access, every bit they  don't demand an object. One of the pop instance of static method is though main method, which deed every bit entry indicate for Java programs.

 i of the oftentimes asked dubiety from Java beginner Difference betwixt static vs non static method inwards Java

Here is the Java programme to highlight duet of differences betwixt static in addition to non static methods inwards Java :

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
  * Java programme to present about differences betwixt static in addition to non static methods inwards 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 tin laissez passer notice telephone telephone static method directly, wihtout creating whatever object
        StaticMethodTest.staticMethod();
     
        // You demand an object to telephone telephone non static mehtod inwards Java
        StaticMethodTest myObject = new StaticMethodTest();
        myObject.nonStaticMethod();
   
    }
 

    public void nonStaticMethod(){
        logger.info("I am a non static method inwards Java");
     
        // You tin laissez passer notice access static variable within non static method
        logger.info("static version from non static method " + version);
    }
 
 
    public static void staticMethod(){
        logger.info("I am a static method inwards Java, version : " + version);
     
        //System.out.println(name); // compile fourth dimension error
    }
}

Output:
2019-07-01 04:10:08,029 0    [main] INFO  StaticMethodTest  - I am a static method inwards Java, version : 0
2019-07-01 04:10:08,060 31   [main] INFO  StaticMethodTest  - I am a non static method inwards Java
2019-07-01 04:10:08,060 31   [main] INFO  StaticMethodTest  - static version from non static method 0


That's all on difference betwixt static in addition to non static methods inwards Java. You tin laissez passer notice come across that non static members are non accessible within static context in addition to y'all too demand to create an object, earlier calling a static method, due to this argue static methods are to a greater extent than suited every bit utility method e.g. Arrays.deepEquals().

Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming inwards Java
Java - Object Oriented Programming [For Absolute Beginners]

Belum ada Komentar untuk "Difference Betwixt Static Vs Not Static Method Inwards Java"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel