Summary of static:
Detail about Static:
1. It is modifier can be applied to variables, methods, blocks, inner class.
2. Static member belongs to class not to the object.
3. Static variables are one copy storage. (Example is given below)
4. Static methods can’t override.
5. Static variables/methods can be called by using class name.
6. Static member gets first preference while allocating memory.
7. Non-static can’t calls from static context.
8. Property is share to all objects.
9. These are memory efficient i.e. Members called without creating an object of class. (Example is given below)
10. Static member get loaded when class is loaded.
11. Static member stores into memory before object creation while
Non-static are loaded after object creation.
Non-static are loaded after object creation.
12. Static blocks executes first then non-static and then constructor. (Example is given below)
13. Not applicable to outer class, constructor, and local variable hence local variables can’t be static.
14. We should know when to use static else it may cause problems like: deadlocks, Race condition (multithreading).
15. “this” and “super” can NOT use in static context.
16. E.g. “Entry” is inner static class present in Hashmap and Hashtable class which mainly consist: key (k), value (v), hash, Entry<k,v>next.
Examples:
1. One
copy Storage
class ABC{
int a=10; static int b=10;
public static void main(String[] args) {
ABC a1=new ABC();
a1.a++;
a1.b++;
System.out.println(a1.a); //output:11
System.out.println(a1.b);// 11
System.out.println("after again object
creation");
ABC a2=new ABC();
a2.a++;
a2.b++;
System.out.println(a2.a);//11
System.out.println(a2.b);//12
}
}
2.
Saving memory/ memory management:
class ABC{
int i=10; static int j=15;
public static void main(String[] args) {
System.out.println(ABC.j); //15
System.out.println(ABC.i);// error for non-static member
}
}
3.
Static
method can NOT override:
class A{
public static void m1() {
System.out.println(" m1 in A");
}
}
class B{
public static void m1() {
System.out.println(" m1 in B");
}
public static void main(String[] args) {
A
a=new A();// m1
in A
a.m1();
B
b=new B();//m1
in B
b.m1();
A
a1=new B();//can't
override
a1.m1();
}
}
4.
Avoid null pointer execution:
class A {
static String str= "java";
static void m1() { // if
static removed then gives null pointer exception
System.out.println("in static method m1");
}
public static void main(String[] args) {
A a1=null;
a1.m1();
System.out.println(a1.str);
}
}
Output:
in static method m1
java
5. Execution of Static block, Non-Static Block and Constructor:
5. Execution of Static block, Non-Static Block and Constructor:
package abc;
public class ExecuteSeq {
ExecuteSeq () {
System.out.println("Constructor
called!");
}
{ //Non-Static Block
System.out.println("Non-Static block
called!");
}
static { //Static Block
System.out.println("Static block
executed!");
}
public static void main(String[] args) {
ExecuteSeq seq1 = new ExecuteSeq();
ExecuteSeq seq2 = new ExecuteSeq();
}
}
Output:
Static block executed!
Non-Static block called!
Constructor called!
Non-Static block called!
Constructor called!
- In above example, Static block executed only ONCE no matters how many objects has been created.
- Non-Static block and Constructor called when object is created. Here it is called 2 times as two objects has been created.
- Sequence of an execution is 1. Static block, 2. Non-Static block, 3. Constructor.
No comments:
Post a Comment
**** Please let me know if you have any doubts. ****