Why we declare static variable in a class & the definition in outside of the class?

Vicky

We declare a static variable in a class and initialize the variable outside the class, but we use the variable within the function.

Anyone tell me the reason why? Thanks in Advance

Abhishek Bansal

I'm not sure but my guess is, because inside a class member variables are only declared. They are initialized through a constructor or other member functions.

This happens when an object is instantiated. However for static members, the objects need not be instantiated. Hence they need to be initialized once outside of the class.

EDIT:
Actually it is not necessary to initialize the static variables, but it is necessary to define them outside the class to allocate memory for them. Only after their definition, can they be initialized and then used in the program.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python - declare method name in dictionary with method definition in outside class

From Dev

PHP: static variable outside of class

From Dev

Why can't I declare and initialize static variable in inner class?

From Dev

Whether redeclare a const static variable outside a class or not

From Dev

Why declare self static instance of a class

From Dev

static_assert inside/outside class definition

From Dev

Variable assignment outside class definition

From Dev

Why is the static variable initialized with redeclaration of static variable outside the class? Can't we just initialize it instead of redeclaring it?

From Dev

how to declare uninitialized variable in class definition in python

From Dev

Declare method outside of class

From Dev

Partial Template specialization definition outside of class definition

From Dev

const static member initialization - inside vs outside class definition

From Dev

friend not allowed outside of a class definition

From Dev

Declare a static variable in an enum class

From Dev

Swift: How to declare a static member variable which is a class

From Dev

Why we can't declare whole class as synchronized in Java?

From Dev

Declare a class property outside of a class method

From Dev

Can we declare member variable of JPA static metamodel class as a final?

From Dev

Why do we need to define a static variable of a class, unlike the other member of a class?

From Dev

Why should we change the modifiers of fields outside an inner class to final?

From Dev

Python - declare method name in dictionary with method definition in outside class

From Dev

Access public static variable outside the class

From Dev

PHP Declare class with a variable

From Dev

Declare variable of a generic class

From Dev

Declare a variable class

From Dev

Partial Template specialization definition outside of class definition

From Dev

Why aren't we allowed to access a private static member outside class without public member function?

From Dev

How to declare a static variable with class.js?

From Dev

Move function outside class definition

Related Related

  1. 1

    Python - declare method name in dictionary with method definition in outside class

  2. 2

    PHP: static variable outside of class

  3. 3

    Why can't I declare and initialize static variable in inner class?

  4. 4

    Whether redeclare a const static variable outside a class or not

  5. 5

    Why declare self static instance of a class

  6. 6

    static_assert inside/outside class definition

  7. 7

    Variable assignment outside class definition

  8. 8

    Why is the static variable initialized with redeclaration of static variable outside the class? Can't we just initialize it instead of redeclaring it?

  9. 9

    how to declare uninitialized variable in class definition in python

  10. 10

    Declare method outside of class

  11. 11

    Partial Template specialization definition outside of class definition

  12. 12

    const static member initialization - inside vs outside class definition

  13. 13

    friend not allowed outside of a class definition

  14. 14

    Declare a static variable in an enum class

  15. 15

    Swift: How to declare a static member variable which is a class

  16. 16

    Why we can't declare whole class as synchronized in Java?

  17. 17

    Declare a class property outside of a class method

  18. 18

    Can we declare member variable of JPA static metamodel class as a final?

  19. 19

    Why do we need to define a static variable of a class, unlike the other member of a class?

  20. 20

    Why should we change the modifiers of fields outside an inner class to final?

  21. 21

    Python - declare method name in dictionary with method definition in outside class

  22. 22

    Access public static variable outside the class

  23. 23

    PHP Declare class with a variable

  24. 24

    Declare variable of a generic class

  25. 25

    Declare a variable class

  26. 26

    Partial Template specialization definition outside of class definition

  27. 27

    Why aren't we allowed to access a private static member outside class without public member function?

  28. 28

    How to declare a static variable with class.js?

  29. 29

    Move function outside class definition

HotTag

Archive