Static & Non Static Block in java

Statement :

  • Statement is a complete unit of execution.
int a =5;

Block:

  • A block is a group of statements (zero or more) that is enclosed in curly braces { }.
{
System.out.println("Hi");
System.out.println("Hello");
}

Diff b/w method & block:

METHOD BLOCK
A method is set of instructions grouped together to perform specified task in the classBlock is a group of statements enclosed in curly braces.
Methods have methodname, arguments, returntype.Blocks doesnot have blockname, arguments, returntype.
Methods are (generally) named and can be called from elsewhere in code by that name.Blocks cannot, they can only be reached by following the flow of the code in the class.
Methods cannot executed first before blocks.Blocks are executed first.
Difference between method & block in java

Static Block:

  • Static: Class Specific statement.
  • Static blocks are the blocks with static keyword.
  • Static blocks execute when a class is initialized.
  • Static blocks are used for initialize the static variables.
  • Static block statement executes before main method & constructors.
  • Static block not executed without main method.
  • Static block gets executed every time when the class loads into memory not every time an object is created.
  • We can create any number of static blocks.
static
{
System.out.println("Hi");
System.out.println("Hello");
}

Non-Static Block:

  • Non-Static Block: Object Specific Statement.
  • Non-Static blocks are the blocks without keyword.
  • Non-Static blocks are used for initializing non static variables.
  • When ever object created non static blocks will be executed before the execution of constructor.
  • Non static blocks are automatically called when object created.
  • We can create any number of Non static blocks.
{
System.out.println("Hi");
System.out.println("Hello");
}

Program:

public class Student1 
{
static String name;
static int age;

static
{
name = "Rajalakshmi";
age = 15;
System.out.println("Initialization - Static block 1");
}

static
{
System.out.println("Initialization - Static block 2");
}

public static void main(String args[])
{
Student1 std = new Student1();
System.out.println(Student1.name);
System.out.println(Student1.age);
}

}

Output:

Initialization – Static block 1
Initialization – Static block 2
Rajalakshmi
15


Program:

public class Student1 
{
String name;
int age;

{
name = "Rajalakshmi";
age = 15;
System.out.println("Initialization -Non Static block 1");
}

{
System.out.println("Initialization -Non Static block 2");
}

public static void main(String args[])
{
Student1 std = new Student1();
System.out.println(std.name);
System.out.println(std.age);
}

}

Output:

Initialization -Non Static block 1
Initialization -Non Static block 2
Rajalakshmi
15


Program:

public class Practise 
{

static
{
System.out.println("Hello! This is a static block 1");
}

{
System.out.println("Hello! This is a non-static block 1");
}

Practise()
{
System.out.println("Hello! This is a constructor");
}
   
public static void main(String args[])
{
System.out.println("Hello! This is a Main Method"); 
Practise workout1 = new Practise();
Practise workout2 = new Practise();
}

static
{
System.out.println("Hello! This is a static block 2");
}

{
System.out.println("Hello! This is a non-static block 2");
}

}

Output:


Program without main method():

public class Learning
{

static
{
System.out.println("Self Learning");
}

}

Compilation Error:

  • Main method not found in class Learning, please define the main method as:
    • public static void main(String[] args)
    • or a JavaFX application class must extend javafx.application.Application

Solution:

public class Learning
{

static
{
System.out.println("Self Learning");
}

public static void main(String[] args)
{
System.out.println("Main Method");
}

}

Output:

Self Learning
Main Method


Leave a comment

Design a site like this with WordPress.com
Get started