answersLogoWhite

0


Best Answer

Static member variables of a class are variables that are local to the class within which they are declared. That is, each instance of the class (each object) shares the same common member variable. Changing that variable's value in one instance will change it for all instances.

Contrast with a non-static member variable where each instance of the class has its own independent variable, scoped to the object itself (not the class of object).

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

All static variables default to zero unless otherwise specified in their declaration.

void foo()

{

static int x; // defaults to zero.

static int y=20; // defaults to 20.

//...

}

Static variables are initialised (once) when the program is executed and remain in scope until the program terminates.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

The default value of any variable is determined by the default constructor for the variable's type. Numeric types, such as int, float and complex, default to the value 0. Strings default to an empty string. Containers default to empty containers, and so on.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Type dependent: 0, 0.0, NULL...

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is static variable in the class in cpp?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is static variable in the class for c plus plus?

Static member variables are local to the class. That is, there is only one instance of a static member variable, regardless of how many objects are instantiated from the class. As such, they must be declared inside the class, and defined outside of the class.


What are static objects in java?

There is no separate entity as a static object in java. The static keyword in java is used to signify that the member (either a variable or a method) is not associated to an object instance of the class. It signifies the fact that the member belongs to the class as a whole. The words static and objects are opposites of one another so you cannot have a static object. However, you can declare an object as a class level variable which could be referred to as a static object but it will be referred to as a static or class variable and not a static object.


What is static java most commonly used for?

Static java method is the same as a static variable. They belong to a class and not an object of that class. If a method needs to be in a class, but not tied to an object, then one uses static java.


What is static char in C?

static storage class in C tells that: The variable will have the default value as zero. The variable scope will be the file in which it is defined. RaVi


How do you create an static int as an instance variable?

You create a static integer as an instance variable by declaring it in a class using a statement that consists of the privacy level, then the keywords "static" and "int", and finally, the name of the variable. For example:public class TheAnswerIsHere {public static int example = 0;}will define an int example with initial value 0. The variable is accessed through the statement TheAnswerIsHere.example.

Related questions

What is the static variable in the class in c?

A static member variable is local to the class rather than to an object of the class.


What is static variable in the class for c plus plus?

Static member variables are local to the class. That is, there is only one instance of a static member variable, regardless of how many objects are instantiated from the class. As such, they must be declared inside the class, and defined outside of the class.


What is importance of static variable?

The static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.


What are static objects in java?

There is no separate entity as a static object in java. The static keyword in java is used to signify that the member (either a variable or a method) is not associated to an object instance of the class. It signifies the fact that the member belongs to the class as a whole. The words static and objects are opposites of one another so you cannot have a static object. However, you can declare an object as a class level variable which could be referred to as a static object but it will be referred to as a static or class variable and not a static object.


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.


What is static java most commonly used for?

Static java method is the same as a static variable. They belong to a class and not an object of that class. If a method needs to be in a class, but not tied to an object, then one uses static java.


What is static char in C?

static storage class in C tells that: The variable will have the default value as zero. The variable scope will be the file in which it is defined. RaVi


Can you use this operator to refer static variable?

No. A static member variable is local to the class in which it is declared (much like a global, but scoped to the class) and is accessible to all instances of the class. Since it does not belong to any one instance of the class, it cannot be accessed via the this pointer, as you can with non-static members. Implicitly accessing the variable is the same as explicitly accessing it via ::.Note that it is possible to access a static member variable from outside the class by providing an accessor (getter) for it within the class. The accessor should be static as well, but needn't be, but it should return by value, otherwise it is no better than a global.


What is difference between an instance variable and a class variable?

An instance variable is typically associated with an object instance of the class whereas class variable is not associated with any object instance. Static variables are referred to as class variables while non-static regular variables are called instance variables. Simply put, you will have as many instances of the instance variable as there are object instances. i.e., if there are 10 instances of an object, you will have 10 instances of that instance variable as well. But, there will be only one instance of the static or class variable. Instance variables are accessed as follows: objname.variableName; Class variables are accessed as follows: ClassName.variableName;


What is meant by a static variable?

A static variable has load module lifetime. Its value persists until changed or the program exits. If the variable is at file scope, i.e. outside of any block, the word static means that it is visible only to code within that compilation unit, i.e. it can not be linked by other compilation units. In a C++ class, a static variable is common to all instances of the class.


How do you create an static int as an instance variable?

You create a static integer as an instance variable by declaring it in a class using a statement that consists of the privacy level, then the keywords "static" and "int", and finally, the name of the variable. For example:public class TheAnswerIsHere {public static int example = 0;}will define an int example with initial value 0. The variable is accessed through the statement TheAnswerIsHere.example.


Is it true that class variables can be assessed without creating an object of the class?

Yes, if the variable is static. When a variable is static, is associated with the class itself and not any particular object (though it can be accessed through an object as well). An example is shown below: class A { static int k = 5; } ... System.out.println(A.k);//5