Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

Chapter#1 Blocks FAQs


1. What is Block?
A block is a group of statements .
2. How many types blocks in java?
There are two types of blocks in Java
a. Static block : executed one time per class
b. Instance Initializer block: executed one time per object
3. What is the syntax of writing static block?
static {
//statements
}
4. What is the syntax of instance block?
{
//statements
}
5. Who will execute/call blocks?
Blocks are auto-executable by JVM. Programmer not required call
externally like methods or constructors.
6. When static block in executed/called?
At the time of loading class and only once.
7. When instance block is executed/called?
For every object creation and before constructor code.
When Object is created, First Class Loaded-> static block called (one
time) -> then instance block is called -> next constructor is called.
8. Can we write multiple blocks (instance/static) in one class?
Yes, Possible. We can define multiple blocks anywhere in class
9. What is the execution order of multiple blocks?
Based on coding order of blocks they are executed.
10. Can we pass Input to block?
A block never takes input like methods or constructor.
11. Can a block return value?
A block never returns a value like method.
12. What kind of statements we can write block?

1|Page
- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

All types of valid statements which are possible in methods and


constructors. Even Object creations and method callings also.
13. How block is different from method or constructor?

Method Constructor Block


Contains one Name, Name must be class Contains no name
any name Name
Can have Can have only No Parameters and
Parameters and Parameters return type
return type
Must be called by Must be called by Auto called/
programmer programmer executed by JVM

14. Can we create variable in block ?


15. Can we perform Calculations (+,-,*,/) in block?
16. Can we write Conditional statements? (if,switch) in block?
17. Can we write loops (for, while, do-while, foreach..) in block?
18. Can We call method? Can we create object in a block ?
19. Can we call a method in block?
20. Can we write block inside another block? (Blocks Nesting)
21. When we should use static blocks?
22. When we should use instance blocks?

Exercise # Find Outputs


Example#1
public class Sathya {

static {
System.out.println("Welcome to Sathya Technologies ");
}

2|Page
- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

public static void main(String[] args) {


System.out.println("Welcome to Java class");
}
}
Output:__________

Example#2
public class Student{
{
System.out.println("Welcome to RAGHU sir Class ");
}

public static void main(String[] args) {


System.out.println("Welcome to Java class");
}
}
Output:____________

Example#3
public class Person {
static {
System.out.println("Welcome to Sathya Technologies ");
}

{
System.out.println("Welcome to Raghu Sir class");
}

public static void main(String[] args) {


Person p1=new Person();
Person p2=new Person();
Person p3=new Person();
}
}
Output:______________
3|Page
- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

Example#4
public class Product {

static {
System.out.println(" You Are In ");
}
static {
System.out.println(" Sathya Techonolgies ");
}
static {
System.out.println(" All the best ");
}
public static void main(String[] args) {
System.out.println(" Welcome to Java class ");
}
}
Output:______________

Example#5
public class Employee {

{
System.out.println(" Learn Java ");
}
{
System.out.println(" From Raghu Sir ");
}
{
System.out.println(" Get Job at Software Company ");
}
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
}
4|Page
- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

}
Output:_______________

Example#6
public class Course {
static {
System.out.println(" Hello Student! ");
}

{
System.out.println(" Learn Java Course ");
}

public static void main(String[] args) {


new Course();
new Course();
}

{
System.out.println(" From Raghu Sir. ");
}
static {
System.out.println(" Get Job at Software Company. ");
}
}

Output:____________

Example#7
public class Sample {
{
System.out.println(" Learn Java Course ");
}
public static void main(String[] args) {
new Sample() {
5|Page
- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

{
System.out.println(" From Raghu Sir.");
}
};
}

static {
System.out.println(" Sathyatechnologies, Ameerpet ");
}
}

Output:____________

Example#8
public class Contract {
public static void main(String[] args) {
new Contract () {
{
System.out.println(" From Raghu Sir.");
}
static {
System.out.println(" Hi User !!");
}
};
}
}

Output:____________

Example#9
public class Raghu{
{
System.out.println(" Welcome to");
}
public static void main(String[] args) {
6|Page
- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

new Raghu() {
{
System.out.println(" From Raghu Sir.");
}
{
System.out.println(" All the best!! ");
}
};
}
{
System.out.println(" Java Class ");
}
}

Example#10
public class Admin {
static {
System.out.println(" From Raghu Sir! ");
}
{
System.out.println(" Welcome to All");
}
public static void main(String[] args) throws Exception {
Class.forName("Admin");
}
static {
System.out.println(" Java course!! ");
}
{
System.out.println(" All the best!! ");
}

7|Page
- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

Exercise # Valid-Invalid Statements


1. Writing blocks in class is optional
2. Class must have one instance block
3. Instance block is executed only one time per class
4. For three objects static block is called one time
5. instance executed before static block
6. We can not write multiple blocks in one class
7. Block should contain name
8. Block can never have parameters
9. Method call is not possible in block
10. We can create objects in blocks
11. Blocks are auto-executed by JVM
12. Multiple blocks are executed in coding order
13. Instance block is called one time per object
14. Before main() method static block is called
15. Java compiler provides default static block if no block is written
by programmer
16. Instance block is executed before constructor
17. Block can have zero statements also (empty block)
18. Block must be called by programmer only.
19. Static block name must be same as class name
20. static keyword is used to create static block
21. instance keyword is used to create instance block
22. instance block name must be same as class name
23. instance block can also be defined in inner classes
24. static block can not be defined in inner class
25. Blocks are called/executed before methods only
26. Block are called after creating object only
27. Block can be created/defined in abstract class also
28. Blocks are never defined in interfaces

8|Page
- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

Exercise # Match the following


1. Blocks contains (__) a. Called by JVM
2. instance block (__) b. Must be executed in order
3. Static block (__) c. Zero or more block
4. Blocks are (__) d. Called once per object
5. Class can have (__) e. Called while loading class
6. Multiple blocks (__) f. Any type of statements

Exercise # Fill the blanks


1. static blocks are executed while ____________
2. ____ block are executed once per object
3. Multiple blocks are executed in _________
4. ___ block is executed before _______ block
5. For 3 object static block is executed _____ times
6. For 4 objects ___ block is executed 4 times
7. Static block is defined using ______ keyword.
8. ____ calls/executes blocks automatically.
9. ____ blocks can be written in interface.

9|Page
- by RAGHU SIR [Sathya technologies, Ameerpet, Hyderabad ]

Exercise # Programming
1. Write class ‘A’ with one static block and print hello message
2. Write class ‘B’ with instance block and print welcome message
3. Write a java class ‘A’ with static block that prints SathyaTech 10 times

4. Write class ‘D’ with one static block(print hello) and one instance
block (print Hi)

5. Write a java class have 3 instance blocks which prints


Block#1 Hello to All !
Block#2 Welcome to Java Class !
Block#3 From Raghu Sir !

6. Write java class ‘A’ having 2 static block prints ‘hi’,’welcome’ and one
instance block prints ‘bye’

7. Create A class with m1() method that prints ‘Hello M1’. Create A
class object in B class and call A class m1() method in static block of
B class.

8. Create P class with m2() method that prints ‘Sathyatech’. Create B


class with one instance block that has A class object and call m2().

9. Create class Connection with get() method which print ‘Oracle Conn’.
Create object of Connection class in DbOperation class and call get()
method using static block.

10. Create Admin class with show() static method. Call show()
method in Admin static block.

FB: https://www.facebook.com/groups/thejavatemple/
email: javabyraghu@gmail.com

10 | P a g e

You might also like