---
title: "Static block in Java not executed"  
description: "Static block in Java not executed"  
author: "Anonymous User"  
published: 2013-06-06  
updated: 2013-06-06  
canonical: https://www.mindstick.com/forum/1003/static-block-in-java-not-executed  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Static block in Java not executed

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances),\

```
class Test{    public static void main(String arg[]){            System.out.println("**MAIN METHOD");        System.out.println(Mno.VAL);//SOP(9090);        System.out.println(Mno.VAL+100);//SOP(9190);    }}class Mno{    final static int VAL=9090;    static{        System.out.println("**STATIC BLOCK OF Mno\t:"+VAL);    }}
```

**\**I know that a [static block](https://www.mindstick.com/interview/2364/what-is-static-block) executed when class loaded. But in this case the [instance variable](https://www.mindstick.com/forum/23344/multidimensional-array-of-nsinteger-as-instance-variable) inside class Mno is final, because of that the static block is not executing.\
Why is that so? And if I would [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) the final, would it work fine?\
Which [memory](https://www.mindstick.com/blog/300050/what-causes-sudden-memory-loss) will be allocated first, the static [final variable](https://www.mindstick.com/interview/2386/what-is-blank-final-variable-in-java) or the static block?\
If due to the final [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) modifier the class does not get loaded, then how can the variable get memory?\
Please help.Thanks in advance. \

## Replies

### Reply by AVADHESH PATEL

Hi,\
1)Actually you have not extends that Mno class so when compilation start it will generate constant of [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) VAL and when execution start when that variable is needed its load thats from memory.so its not required your class reference so that [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods) bock is not executed.\
2)if A class extend that Mno class at that time that static block is included in A class if you do this then that static block is executed. for example.. public class A extends Mno{\

```
public static void main(String arg[]){        System.out.println("**MAIN METHOD");    System.out.println(Mno.VAL);//SOP(9090);    System.out.println(Mno.VAL+100);//SOP(9190);}}class Mno{      final static int VAL=9090;    static`{`        System.out.println("**STATIC BLOCK OF Mno\t:"+VAL);    }}
```

**\**I hope it may be helpful for you.\


---

Original Source: https://www.mindstick.com/forum/1003/static-block-in-java-not-executed

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
