In this article, nosotros volition receive got a await at the difference betwixt static too non-static method inwards Java, ane of the oft asked uncertainty from Java beginner. In fact, agreement static keyword itself is ane of the primary programming fundamental, thankfully it's good defined inwards Java programming linguistic communication . H5N1 static method inwards Java belongs to class, which agency you lot tin telephone vociferation upwards that method past times using cast cite e.g. Arrays.equals(), you lot don't demand to create whatever object to access this method, which is what you lot demand to practice 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 non access non static members within static context, which agency you lot tin non role non static variables within static methods, you lot tin non telephone vociferation upwards non static methods from static ones, all those volition upshot inwards compile fourth dimension error.
Apart from these pregnant differences betwixt static too non static methods, nosotros volition likewise receive got a await at few to a greater extent than points inwards this Java tutorial, amongst a code example, which shows approximately of these differences inwards action.
By the way this is the minute article on static method, inwards showtime article, nosotros receive got learned almost when to role static method inwards Java
That's all on difference betwixt static too non static methods inwards Java. You tin run into that non static members are non accessible within static context too you lot likewise 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]
Similarly you tin non access non static members within static context, which agency you lot tin non role non static variables within static methods, you lot tin non telephone vociferation upwards non static methods from static ones, all those volition upshot inwards compile fourth dimension error.
Apart from these pregnant differences betwixt static too non static methods, nosotros volition likewise receive got a await at few to a greater extent than points inwards this Java tutorial, amongst a code example, which shows approximately of these differences inwards action.
By the way this is the minute article on static method, inwards showtime article, nosotros receive got learned almost when to role static method inwards Java
Static vs Non Static method inwards Java
Let's run into span of differences betwixt static too non static method inwards Java programming language, which is enforced past times linguistic communication itself.
1) I recollect showtime too initiatory off departure betwixt them is that you lot tin telephone vociferation upwards 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 vociferation upwards non static method inwards Java. Static methods are likewise pretty useful on several blueprint pattern including Factory and Singleton.
2) You tin non access a non static variable within whatever static method inwards Java, but opposite is fine i.e. you lot tin access static variables or telephone vociferation upwards static method from a non static method without whatever compile fourth dimension error.
3) One to a greater extent than worth noting departure betwixt static too non static method is that you tin non override static method inwards Java. They are bonded during compile fourth dimension using static binding. Though you lot tin create a similar static method inwards sub class, that is know every bit method hiding inwards Java.
4) static methods are known every bit cast methods, if you lot synchronize static method inwards Java too then they acquire locked using unlike monitor than non static methods e.g. both static too non static methods are locked using unlike monitor inwards Java, too that's why its grave Java error to part a resources betwixt static too 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 human activity every bit entry signal for Java programs.
Here is the Java plan to highlight span of differences betwixt static too non static methods inwards Java :
1) I recollect showtime too initiatory off departure betwixt them is that you lot tin telephone vociferation upwards 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 vociferation upwards non static method inwards Java. Static methods are likewise pretty useful on several blueprint pattern including Factory and Singleton.
2) You tin non access a non static variable within whatever static method inwards Java, but opposite is fine i.e. you lot tin access static variables or telephone vociferation upwards static method from a non static method without whatever compile fourth dimension error.
3) One to a greater extent than worth noting departure betwixt static too non static method is that you tin non override static method inwards Java. They are bonded during compile fourth dimension using static binding. Though you lot tin create a similar static method inwards sub class, that is know every bit method hiding inwards Java.
4) static methods are known every bit cast methods, if you lot synchronize static method inwards Java too then they acquire locked using unlike monitor than non static methods e.g. both static too non static methods are locked using unlike monitor inwards Java, too that's why its grave Java error to part a resources betwixt static too 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 human activity every bit entry signal for Java programs.
Here is the Java plan to highlight span of differences betwixt static too non static methods inwards Java :
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Java plan to demonstrate approximately differences betwixt static too 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 vociferation upwards static method directly, wihtout creating whatever object
StaticMethodTest.staticMethod();
// You demand an object to telephone vociferation upwards 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 too non static methods inwards Java. You tin run into that non static members are non accessible within static context too you lot likewise 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]