Write a java program to demonstrate static variables, methods, and blocks.
We can access static
variables, blocks, and methods without creating object as shown in the below
program.
JVM first check for the main
method and after checking the main method it will execute static block after
that execute remaining code.
public class StaticTest {
static int id=1214;
static{
String name="narendar";
System.out.println("static block
"+name);
}
static void display(){
String group="CSIT";
System.out.println("static method
"+group);
}
public static void main(String[] args) {
System.out.println("static
variable "+id);
display();
}
}
Output:
/*
static block narendar
static variable 1214
static method CSIT
*/
No comments:
Post a Comment