Java Blocks

Blocks in java

Block: A set of statements together is called as block. The set of statements are written inside the flower braces {}.

In java there are two types of Blocks they are :

  1. Instance Block.
  2. Static block.
Instance Block: Instance block is a set of statements written within the flower braces and will execute when an object is created . Instance blocks will not have any name. Instance blocks will completely depends on the object of a class created .

Syntax:

{
    set of statements;
}

Program Using Instance Block


class InstanceBlock
{
InstanceBlock()
{
System.out.println("Location Of Institute : Dilsuknagar");
}
//Instance Block
{
System.out.println("Name Of Institute : Void Main Technologies");
}

}
class InstanceBlockTest
{
public static void main(String[] args) 
{
InstanceBlock ib=new InstanceBlock();
}
}

Save as:

InstanceBlockTest.java

Compile:

javac InstanceBlockTest.java

Run:

java InstanceBlockTest

Output:

Name Of Institute :void Main Technologies

Location Of Institute : Dilsuknagar

Note:
  1. In any program instance block will be executed first later constructor will be executed .
  2. If we have inheritance concept then first super class instance block will be executed then super class constructor will be executed and later sub class block and sub class constructor will be executed. 

Static Block: This blocks will be executed first in the program without depending on the instance of the class. Static block statements are written inside the static block .This block can be defined by using static keyword .

Syntax:

static
{
     set of statements;
}



Program Using Static Block


class StaticBlock
{
StaticBlock()
{
System.out.println("Location of institute : Dilsuknagar");
}
//Instance Block
{
System.out.println("Caption of Institute: For All Technical courses");
}
//Static Block
static
{
System.out.println("Name Of Institute: Void Main Technologies");
}

}
class StaticBlockTest 
{
public static void main(String[] args) 
{
StaticBlock sb=new StaticBlock();
}
}

Save as:

StaticBlockTest.java

Compile:

javac StaticBlockTest.java

Run:

java StaticBlockTest

Output:

Name Of Institute :void Main Technologies

Caption of Institute: For All Technical courses

Location Of Institute : Dilsuknagar

Note:

Static block will be executed first in any program then the priority is for instance block and then constructor will be executed .We can run the program even without a main method using Static block. 

Priority of execution is: 
Static block > instance Block > Constructor 


Program Using Static Block Without "main" method



class StaticBlock
{
static
{
System.out.println("Name Of Institute: Void Main Technologies");

System.out.println("Location Of Institute: Dilsuknagar");

System.exit(0);

}


}


Save as:

StaticBlock.java

Compile:

javac StaticBlock.java

Run:

java StaticBlock

Output:

Name Of Institute :void Main Technologies

Location Of Institute : Dilsuknagar

Note:



Above program can be used to say that a program can execute even without main method.

Comments

Popular posts from this blog

Programs on Inheritance

Arrays In Java

Java Basics