
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is the order of execution of non-static blocks with respect to a constructor in Java?
Whenever an object is created, a non-static block will be executed before the execution of the constructor.
Non-Static Blocks
- The Non-static blocks are class level blocks which do not have any prototype.
- The need for a non-static block is to execute any logic whenever an object is created irrespective of the constructor.
- The Non-static blocks are automatically called by the JVM for every object creation in the java stack area.
- We can create any number of Non-static blocks in Java.
- The order of execution of non-static blocks is an order as they are defined.
Example
public class NonStaticBlockTest { { System.out.println("First Non-Static Block"); // first non-static block } { System.out.println("Second Non-Static Block"); // second non-static block } { System.out.println("Third Non-Static Block"); // third non-static block } NonStaticBlockTest() { System.out.println("Execution of a Constructor"); // Constructor } public static void main(String args[]) { NonStaticBlockTest nsbt1 = new NonStaticBlockTest(); NonStaticBlockTest nsbt2 = new NonStaticBlockTest(); } }
Output
First Non-Static Block Second Non-Static Block Third Non-Static Block Execution of a Constructor First Non-Static Block Second Non-Static Block Third Non-Static Block Execution of a Constructor
- Related Articles
- Non static blocks in Java.
- Sequence of execution of, instance method, static block and constructor in java?
- Static blocks in Java with example
- What is the order of test execution with priority in TestNG?
- What is a static constructor in C#?
- What is the order of execution of tests in TestNG?
- What is the order of execution of TestNG methods?
- Is it possible to create static constructor in java?
- How to call a static constructor or when static constructor is called in C#?
- What is Execution order of Collection Runner in Postman?
- 2A+B+C ----->+E;It is the first-order reaction with respect to A, A, second order with respect to B and zero-order with respect to C. Give the differential rate equation for the reaction.
- What is the purpose of a constructor in java?
- What are the differences between a static block and a constructor in Java?
- What is the difference between static classes and non-static inner classes in Java?
- Can we define a static constructor in Java?

Advertisements