Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created November 17, 2015 10:02
Show Gist options
  • Save rishi93/1413a8035fd3ce601448 to your computer and use it in GitHub Desktop.
Save rishi93/1413a8035fd3ce601448 to your computer and use it in GitHub Desktop.
Static and non-static Blocks
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