340 likes | 1.01k Views
Using Static keyword in JAVA. JAVA @ ICBT Basura Ramanayaka Eshani werapitiya Hasitha Dananjaya. Overview. Introduction How and where do we use static keyword To variables To methods Advantages of using Static keyword Summary. Introduction.
E N D
Using Static keyword in JAVA JAVA @ ICBT BasuraRamanayaka Eshaniwerapitiya HasithaDananjaya
Overview • Introduction • How and where do we use static keyword • To variables • To methods • Advantages of using Static keyword • Summary
Introduction • Static is a keyword that states that all instances of a given class are to share the same variable or method. • This is used for a constant variable or a method that is the same for every instance of a class • This presentation will provide information about uses of Static keyword in JAVA
How and where we use static keyword to variables • Without the “static” keyword, it's called “instance variable”, and each instance of the class has its own copy of the variable. • When a variable is declared with the static keyword, its called a “class variable”. All instances share the same copy of the variable
A common use of static variables is to define "constants“. • A class variable can be accessed directly with the class, without the need to create a instance. • Variables can be declared with the “static” keyword like this. • Example: • static int y=0;
How and where we use static keyword to Methods • Methods declared with “static” keyword are called “class methods”. Otherwise they are “instance methods”. • Methods declared with static cannot access variables declared without static. public class Main { static int xl = 0; int x = 20; public static void m() { x = 150;//non static x cannot be access } public static void main(String[] args) { m(); } }
The following gives a compilation error, unless m method is also static. Or unless it is accessing through object public class Main { public intm (){ int x = 20; return x; } public static void main(String[] args){ Main ws = new Main(); ws.m(); } } public class Main { public intm (){ int x = 20; return x; } public static void main(String[] args) { m (); } } A B Note: There's no such thing as static classses. “static” in front of class creates compilation error.
***Remember that static methods can’t be OVERRIDDEN. But that doesn’t mean that they cant be redefined in a sub class. class Main { static void travel() { System.out.print("A"); } } class main extends subMain { static void travel() { System.out.print("B"); //this is redefinition NOT overridden } }
Advantages of using Static keyword Static variables can be use to hold constants (values that are not changing). Static method’s behavior has NO dependency on the state of an object. That mean methods which are declared as static will always runs the same way.
Documentation. Anyone seeing that a method is static will know how to call it. Any programmer looking at the code will know that a static method can't interact with instance variables, which makes reading and debugging easier. Efficiency. A compiler will usually produce slightly more efficient code because no implicit object parameter has to be passed to the method.
Summary • Q & A