Static is different functionality in Java. It works with out main and It
maintain constant values in static block. Static block executes only once
per class. Initialization block executes once per object.
public class StaticDemo {
StaticDemo(){
System.out.println("StaticDemo: Constructor");
}
public static void methodOne(){
System.out.println("StaticDemo: methodOne: Static Method.");
}
static{
System.out.println("StaticDemo: Static Block 1");
}
{
System.out.println("StaticDemo: Initialization Block 1");
}
public static void main(String[] args) {
System.out.println("StaticDemo: Main Sart");
System.out.println("---------------------");
StaticDemo sd = new StaticDemo();
sd.methodOne();
System.out.println("---------------------");
StaticDemo sd2 = new StaticDemo();
sd2.methodOne();
System.out.println("---------------------");
StaticDemo.methodOne();
System.out.println("---------------------");
System.out.println("StaticDemo: Main End");
}
static{
System.out.println("StaticDemo: Static Block 2");
}
{
System.out.println("StaticDemo: Initialization Block 2");
}
}
OutPut:
StaticDemo: Static Block 1
StaticDemo: Static Block 2
StaticDemo: Main Sart
---------------------
StaticDemo: Initialization Block 1
StaticDemo: Initialization Block 2
StaticDemo: Constructor
StaticDemo: methodOne: Static Method.
---------------------
StaticDemo: Initialization Block 1
StaticDemo: Initialization Block 2
StaticDemo: Constructor
StaticDemo: methodOne: Static Method.
---------------------
StaticDemo: methodOne: Static Method.
---------------------
StaticDemo: Main End
Build Your Own Test Framework
-
[image: Build Your Own Test Framework]
Learn to write better automated tests that will dramatically increase your
productivity and have fun while doing so...
1 hour ago
No comments:
Post a Comment
I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.