Sitemap

Why static Block executes before main method(String[] args)?

2 min readMay 6, 2024

--

Static block code executes only once during the class loading. The static blocks always execute first before the main() method in Java because the compiler stores them in memory at the time of class loading and before the object creation.

Static Block gets Memory allocation before Main method

Static block always gets executed first in Java because it is stored in the memory at the time of class loading and before the object creation.

Summarize of Memory allocations in Java

Press enter or click to view image in full size

What is the use of static class in java?

In Java, a static nested class is a class that is declared as a member of another class and is marked with the static keyword.

By nesting a class within another class, you can logically group related classes together, encapsulating them within the outer class.

What is constructor?

Get Chirag Narang’s stories in your inbox

Join Medium for free to get updates from this writer.

Java constructors are special types of methods that are used to initialize an object when it is created.
Typically, a constructor is used to give initial values to the instance variables defined by the class or to perform any other start-up procedures required to create a fully formed object.

What Are the Rules for Creating Constructors in Java?
There are a total of three rules defined for creating a constructor.

1. The constructor’s and class’s name must be identical.

2. Java constructors do not have a return type. Even do not use void as a return type.

3. There can be multiple constructors in the same class, this concept is known as constructor overloading.

4. A constructor cannot be any of these: static, synchronized, abstract, or final.

5. The access modifiers can be used with the constructors, use if you want to change the visibility/accessibility of constructors.

6. Java provides a default constructor that is invoked during the time of object creation. If you create any type of constructor, the default constructor (provided by Java) is not invoked.

Summarize of Constructor, Static, Private and Main methods

Press enter or click to view image in full size

--

--