Difference Betwixt Static Vs Not Static Method Inwards Java

In this article, nosotros volition guide maintain a await at the difference betwixt static too non-static method inward Java, 1 of the often asked incertitude from Java beginner. In fact, agreement static keyword itself is 1 of the master copy programming fundamental, thankfully it's good defined inward Java programming linguistic communication . Influenza A virus subtype H5N1 static method inward Java belongs to class, which agency you lot tin orbit the axe telephone outcry upwardly that method past times using course of educational activity refer e.g. Arrays.equals(), you lot don't demand to create whatever object to access this method, which is what you lot demand to exercise to access non-static method of a class. Static method is treated differently past times compiler too 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 orbit the axe non access non static members within static context, which agency you lot tin orbit the axe non operate non static variables within static methods, you lot tin orbit the axe non telephone outcry upwardly non static methods from static ones, all those volition number inward compile fourth dimension error.

Apart from these meaning differences betwixt static too non static methods, nosotros volition too guide maintain a await at few to a greater extent than points inward this Java tutorial, amongst a code example, which shows to a greater extent than or less of these differences inward action.

By the way this is the mo article on static method, inward outset article, nosotros guide maintain learned close when to operate static method inward Java




Static vs Non Static method inward Java

Let's come across distich of differences betwixt static too non static method inward Java programming language, which is enforced past times linguistic communication itself.

1) I retrieve outset too firstly deviation betwixt them is that you lot tin orbit the axe telephone outcry upwardly static method without creating whatever object e.g. Collections.sort(). This makes static method useful utility,  spell you lot demand to instantiate an object to telephone outcry upwardly non static method inward Java. Static methods are too pretty useful on several blueprint pattern including Factory and Singleton.

2) You tin orbit the axe non access a non static variable within whatever static method inward Java, but contrary is fine i.e. you lot tin orbit the axe access static variables or telephone outcry upwardly static method from a non static method without whatever compile fourth dimension error.

3) One to a greater extent than worth noting deviation betwixt static too non static method is that you tin orbit the axe non override static method inward Java. They are bonded during compile fourth dimension using static binding. Though you lot tin orbit the axe create a like static method inward sub class, that is know every bit method hiding inward Java.

4) static methods are known every bit course of educational activity methods, if you lot synchronize static method inward Java therefore they acquire locked using unlike monitor than non static methods e.g. both static too non static methods are locked using unlike monitor inward Java, too that's why its grave Java error to portion a resources betwixt static too non static method inward Java.

5) Static methods are ordinarily 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 event of static method is though main method, which deed every bit entry indicate for Java programs.

 1 of the often asked incertitude from Java beginner Difference betwixt static vs non static method inward Java

Here is the Java programme to highlight distich of differences betwixt static too non static methods inward Java :

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

/**
  * Java programme to exhibit to a greater extent than or less differences betwixt static too non static methods inward 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 orbit the axe telephone outcry upwardly static method directly, wihtout creating whatever object
        StaticMethodTest.staticMethod();
     
        // You demand an object to telephone outcry upwardly non static mehtod inward Java
        StaticMethodTest myObject = new StaticMethodTest();
        myObject.nonStaticMethod();
   
    }
 

    public void nonStaticMethod(){
        logger.info("I am a non static method inward Java");
     
        // You tin orbit the axe 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 inward Java, version : " + version);
     
        //System.out.println(name); // compile fourth dimension error
    }
}

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


That's all on difference betwixt static too non static methods inward Java. You tin orbit the axe come across that non static members are non accessible within static context too you lot 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 inward Java
Java - Object Oriented Programming [For Absolute Beginners]


Sumber https://javarevisited.blogspot.com/

0 Response to "Difference Betwixt Static Vs Not Static Method Inwards Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel