Created
November 17, 2015 10:02
-
-
Save rishi93/1413a8035fd3ce601448 to your computer and use it in GitHub Desktop.
Static and non-static Blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Sequence | |
{ | |
/* A static block. Called only once, the first | |
time you are accessing the class. Static block is | |
executed before class is loaded. */ | |
static | |
{ | |
System.out.println("Executing static block.."); | |
} | |
/* Non - static block. Called everytime an instance is created. | |
Called just before calling the constructor */ | |
{ | |
System.out.println("Executing non-static block.."); | |
} | |
/* User defined constructor */ | |
Sequence() | |
{ | |
System.out.println("Executing constructor.."); | |
} | |
/* A Method */ | |
void go() | |
{ | |
System.out.println("Executing instance method.."); | |
} | |
public static void main(String[] args) | |
{ | |
new Sequence().go(); /* Creating one object */ | |
new Sequence().go(); /* Creating second object */ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment