# Variable declared inside a static function

**URL:** <https://verificationacademy.com/forums/t/variable-declared-inside-a-static-function/35005>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [September 7, 2018, 12:13pm UTC](https://verificationacademy.com/forums/t/variable-declared-inside-a-static-function/35005 "2018-09-07T12:13:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ManojJ](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/22d042/32.png) [@ManojJ](https://verificationacademy.com/forums/u/ManojJ)\
**Post date:** [September 7, 2018, 12:13pm UTC](https://verificationacademy.com/forums/t/variable-declared-inside-a-static-function/35005/1 "2018-09-07T12:13:33Z")

</div>

Hi, this is the code i tried to execute,

```verilog

class base;
  static int i;
    static function get();
     int a;
      a++;
      $display(a);
    endfunction
  endclass
module top;
  initial begin
    base b1,b2;
    b1.get();
    b1.get();
    b2.get();
  end
endmodule

```

the output was  
1  
1  
1  
why a is incrementing only once? and can we declare a non static variable inside a static function?  
Can anyone please explain the operation of this code…  
and if I declare the same function as

```verilog
 static function static get()

```

i am getting the output  
1  
2  
3

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [September 7, 2018, 3:04pm UTC](https://verificationacademy.com/forums/t/variable-declared-inside-a-static-function/35005/2 "2018-09-07T15:04:29Z")

</div>

_In reply to [Manoj J](https://verificationacademy.com/forums/t/variable-declared-inside-a-static-function/35005):_

‘a’ is an automatic variable that gets initialized to 0 on each call to get(). You can declare ‘a’ as a static variable if thst is what you want.

The syntax   
static function static was made illegal in the 1800-2009 LRM to prevent the confusion you are having now.

See [automatic variables inside static method | Verification Academy](https://verificationacademy.com/forums/systemverilog/automatic-variables-inside-static-method#reply-42858)

---

<div class="post-metadata">

**Author:** ![ManojJ](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/22d042/32.png) [@ManojJ](https://verificationacademy.com/forums/u/ManojJ)\
**Post date:** [September 8, 2018, 4:53am UTC](https://verificationacademy.com/forums/t/variable-declared-inside-a-static-function/35005/3 "2018-09-08T04:53:44Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/variable-declared-inside-a-static-function/35005/2):_

Thanks dave!!
