Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Static Block in Java

Static Block in Java

Table of Contents

  • Static Block in Java
    • Example
    • Static block
    • Example
    • Multiple static blocks
    • Initializer Block in Java
    • Example using static and initializer block

Static Block in Java

Static Block in Java

The static keyword in Java is primarily utilised for memory management. The static keyword can be used with nested classes, variables, methods, and blocks. A static block in a programme is a collection of statements that the Java Virtual Machine (JVM) runs prior to the main method. So If we want to perform a task at the time the class loads, we can define it inside the static block, and it will be carried out at that time. So Any number of static blocks may be defined in a class, and these static blocks will be executed in the order they are defined.

Syntax:


static {
		**********
		**********
		// statements….
		**********
		**********
	}

	

Example

Static block

When a programme is running, the static block is executed before the main method. The static block’s statements will be executed first. Both, though, remain static.


class StaticDemo1
{
	static
	{
		System.out.println("Welcome to
onlineexamguide.com"); System.out.println("This is static block"); } public static void main(String as[]) { System.out.println("This is main() method"); } }

Example

Multiple static blocks

When there are multiple static blocks, they all run in sequence. The initial static block is the first to run.


class StaticDemo1
{
	static
	{
		System.out.println("Welcome to
onlineexamguide.com"); System.out.println("This is static block I"); } public static void main(String as[]) { System.out.println("**********************"); System.out.println("This is main() method"); } static { System.out.println("**********************"); System.out.println("This is static block II"); } static { System.out.println("**********************"); System.out.println("This is static block
III"); } }

Initializer Block in Java

In Java, instance data members are initialised using the initializer Block. And Every time an object is created, the initializer block is called. So Every constructor receives a copy of the Initializer block after being copied into the Java compiler. The constructor’s code is executed after the initialization block.

Example:


class InitializerDemo1
{
	{
		System.out.println("Welcome to 
onlineexamguide.com"); System.out.println("This is Initializer
block"); } public InitializerDemo1() { System.out.println("Default Constructor
invoked"); } public static void main(String as[]) { InitializerDemo1 obj = new InitializerDemo1(); System.out.println("This is main() method"); } }

Example using static and initializer block

A Java programme can contain both static and initializer blocks. However, the initializer block will not run before the static block. A good example is provided below.


public class one extends two {
    static {
System.out.println("inside satic block");
    }

one() {
System.out.println("inside constructor of child");
    }

    {
System.out.println("inside initialization block");
    }

    public static void main(String[] args) {
        new one();
        new one();
System.out.println("inside main");
    }
}

class two{
    static {
System.out.println("inside parent Static block");
    }
    {
System.out.println("inside parent initialisation block");
    }

two() {
System.out.println("inside parent constructor");
    }
}
	

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.