Difference Betwixt Static Vs Not Static Method Inwards Java
In this article, nosotros volition accept a await at the difference betwixt static together with non-static method inwards Java, i of the often 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 . H5N1 static method inwards Java belongs to class, which way y'all tin telephone telephone that method yesteryear using bird rear e.g. Arrays.equals(), y'all don't demand to create whatsoever 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 yesteryear compiler together with JVM than non-static methods, static methods are bonded during compile time, equally opposed to binding of the non static method, which happens at runtime.
Similarly you tin non access non static members within static context, which way y'all tin non piece of job non static variables within static methods, y'all tin non telephone telephone non static methods from static ones, all those volition effect inwards compile fourth dimension error.
Apart from these meaning differences betwixt static together with non static methods, nosotros volition too accept a await at few to a greater extent than points inwards this Java tutorial, amongst a code example, which shows roughly of these differences inwards action.
By the way this is the minute article on static method, inwards origin article, nosotros receive got learned virtually when to piece of job static method inwards Java
That's all on difference betwixt static together with non static methods inwards Java. You tin run across that non static members are non accessible within static context together with 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 equally 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]
Similarly you tin non access non static members within static context, which way y'all tin non piece of job non static variables within static methods, y'all tin non telephone telephone non static methods from static ones, all those volition effect inwards compile fourth dimension error.
Apart from these meaning differences betwixt static together with non static methods, nosotros volition too accept a await at few to a greater extent than points inwards this Java tutorial, amongst a code example, which shows roughly of these differences inwards action.
By the way this is the minute article on static method, inwards origin article, nosotros receive got learned virtually when to piece of job static method inwards Java
Static vs Non Static method inwards Java
Let's run across couplet of differences betwixt static together with non static method inwards Java programming language, which is enforced yesteryear linguistic communication itself.
1) I recall origin together with initiative of all departure betwixt them is that y'all tin telephone telephone static method without creating whatsoever 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 blueprint pattern including Factory and Singleton.
2) You tin non access a non static variable within whatsoever static method inwards Java, but contrary is fine i.e. y'all tin access static variables or telephone telephone static method from a non static method without whatsoever compile fourth dimension error.
3) One to a greater extent than worth noting departure betwixt static together with non static method is that you tin non override static method inwards Java. They are bonded during compile fourth dimension using static binding. Though y'all tin create a like static method inwards sub class, that is know equally method hiding inwards Java.
4) static methods are known equally bird methods, if y'all synchronize static method inwards Java together with then they acquire locked using dissimilar monitor than non static methods e.g. both static together with non static methods are locked using dissimilar monitor inwards Java, together with that's why its grave Java error to part a resources betwixt static together with 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, equally they don't demand an object. One of the pop event of static method is though main method, which human activity equally entry betoken for Java programs.
Here is the Java computer programme to highlight couplet of differences betwixt static together with non static methods inwards Java :
1) I recall origin together with initiative of all departure betwixt them is that y'all tin telephone telephone static method without creating whatsoever 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 blueprint pattern including Factory and Singleton.
2) You tin non access a non static variable within whatsoever static method inwards Java, but contrary is fine i.e. y'all tin access static variables or telephone telephone static method from a non static method without whatsoever compile fourth dimension error.
3) One to a greater extent than worth noting departure betwixt static together with non static method is that you tin non override static method inwards Java. They are bonded during compile fourth dimension using static binding. Though y'all tin create a like static method inwards sub class, that is know equally method hiding inwards Java.
4) static methods are known equally bird methods, if y'all synchronize static method inwards Java together with then they acquire locked using dissimilar monitor than non static methods e.g. both static together with non static methods are locked using dissimilar monitor inwards Java, together with that's why its grave Java error to part a resources betwixt static together with 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, equally they don't demand an object. One of the pop event of static method is though main method, which human activity equally entry betoken for Java programs.
Here is the Java computer programme to highlight couplet of differences betwixt static together with non static methods inwards Java :
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Java computer programme to demo roughly differences betwixt static together with 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 telephone telephone static method directly, wihtout creating whatsoever 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 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:
2013-07-01 04:10:08,029 0 [main] INFO StaticMethodTest - I am a static method inwards Java, version : 0
2013-07-01 04:10:08,060 31 [main] INFO StaticMethodTest - I am a non static method inwards 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 together with non static methods inwards Java. You tin run across that non static members are non accessible within static context together with 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 equally 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]

Komentar
Posting Komentar