answersLogoWhite

0


Best Answer

Static members should be used whenever you have a member that is logically considered part of the class, but is not associated with any instance of the class. They are bit like global variables and functions but there's more control over their visibility outside of the class.

Static member methods can be thought of as being global functions that are scoped to the class (rather than to each instance of the class). As such, they do not inherit an implicit this pointer and can therefore be called without instantiating an instance of the class (access specifier permitting). However, any and all instances of the class have unrestricted access to all the static member methods of the class.

Static member variables can be thought of as being global variables that are scoped to the class (rather than to each instance of the class). That is, all instances of the class share the same set of static member variables amongst them, rather than each having their own (as they would with non-static member variables). Static member variables are also accessible to static member functions. As with all other static variables, they are initialised at compile time (which must be done outside of the class declaration), and will remain in scope for the entire lifetime of the program.

All static members are subject to the public, protected or private access specifiers, so while they can act like global variables and functions if declared public, their visibility outside of the class can be limited to private or protected access.

Possible uses for static members are many and varied. By way of an example, suppose you want to maintain a read-only count of all instances of a class. The following framework demonstrates how this can be done with static members:

class Object

{

public:

Object(){++s_instances;}

~Object(){--s_instances;}

static unsigned int GetInstances(){return(s_instances);}

private:

static unsigned int s_instances;

};

// Static member variable initialiser (evaluated at compile time):

unsigned int Object::s_instances=0;

int main()

{

cout<<"Current instances:\t"<

Object * p1 = new Object();

cout<<"Current instances:\t"<

Object * p2 = new Object();

cout<<"Current instances:\t"<

delete( p1 );

cout<<"Current instances:\t"<

delete( p2 );

cout<<"Current instances:\t"<

}

Output:

Current instances: 0

Current instances: 1

Current instances: 2

Current instances: 1

Current instances: 0

Previous Answer

In the code, if you want to have only one instance of the member variable irrespective of the number of objects created for that class. In these kind of scenarios static members are used

User Avatar

Wiki User

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

Wiki User

14y ago

In C++, a static class member is common to all instances of that class. A static class method is a method that can only access static class members. Non static methods can also access static members, but remember that there is only one instance of that member amongst all instances of the class. One advantage of a static method is that you do not have to instantiate the class to use the static member or static method.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

You declare a member of a class to be static when the member is local to the class as a whole, rather than to an instance of the class. Static members are a bit like global variables or functions, but their scope is limited to class scope rather than global scope (although public static members are essentially global). As members of the class, however, static member functions also have private access to the class, including other static members as well as class instance members. But they do not have access to the implicit this pointer since they are not associated with any instance. To access a class instance member, a static member function must either accept one or more class instances via the function's arguments, and/or instantiate one or more new instances within the body of the function.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

When all objects share a common private data variable, it is declared as static. Look at the example here

Class example

{

private:

static int counter;

int sales;

public:

example();

~example();

void example(...);

void increment();

void decrement();

};

static int counter=0; // initialized only once.

void example :: void increment()

{

counter++;

}

void example:: void decrement()

{

counter--;

}

----

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

If you declare a field static, there will only be one copy of it in the entire program, even if several objects are created based on the corresponding class. It is up to the programmer to decide when this would be appropriate.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Whenever the member should be local to the class rather than local to every instance of the class. All non-static members are instance members. As such, instance member methods have access to the this pointer. Static member methods do not. However, static members exist for the duration of a program's life-cycle and are accessible even when no instances of the class exist.

Private static members are visible to all members of the class as well as to friends of the class. Protected static members are also visible to derivatives of the class. Public static members are akin to global variables and functions, but are localised to the class namespace.

Static member data can also be thought of as being shared data that is common to all instances of the class. For instance, if you need to maintain a count of all instances of a class, a private static size_t member could be used, initialised to zero. Every constructor increments the member while the destructor decrements the member. A public static accessor (getter) method can be used to return the value of the static member as a read-only variable.

Note that all static member data must be initialised outside of the class declaration and outwith the class' implementations. Typically, initialisation code is placed in a corresponding source file immediately after including the class header but before defining the class implementations. However, if the class is a template class, the declaration and definition, including static data initialisation, must be completely visible to the compiler. In this case, the header must include the file containing the source file rather than the other way around. Alternatively, all code may be placed in the header itself, but this makes it more difficult to hide the implementation details from consumers.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

We declare a member static when the member is to be regarded as being local to the class rather than local to each instance of the class. That is; there is only ever one instance of the member rather than one per object. We use static member variables for data that is shared amongst all instances of the class. Fort that reason, non-static member variables are often called instance variables since each instance has its own discrete set of non-static member data. Static member functions, on the other hand, are the same as non-static member functions except that they do not have access to an implicit this pointer. As such, public static member functions can be invoked even when there are no objects of the class in memory.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

If a member field (variable) of a class is static, a single copy of the field exists for the entire class, no matter how many objects you create for the class.

Apart from saving space, this makes it possible to share data among different objects. For a simple example, you might want to assign each object a serial number - the first object created will be #1, the second #2, etc. This requires some variable, that will be incremented every time an object is created. An instance (object) variable doesn't work in this case, because each instance (object) will have a separate copy of the instance variable, and set it to a default value, such as zero.

In languages where no static members (or something equivalent) exist, this would require having a variable, separate from the class. This works, but it is a little more messy - the whole idea of classes is to keep things organized; so variables that are related to the class should logically be defined in the class.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

yes u can declare a class static if static class is present in ur program then it should be executed first

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When do you declare a member of a class static?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How you declare class scoped variables and member functions?

To scope class members to the class (rather than to instances of the class), declare them as static members of the class. Static members are accessible even when no instances of the class exist. As such, static member functions do not have access to a 'this' pointer, unlike ordinary (nonstatic) member functions.


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 static data member and ordinary data member?

Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.


How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


What is members in java?

A member class is a class that is declared as a non-static member of a containing class. If a static member class is analogous to a class field or class method, a member class is analogous to an instance field or instance method.by k7

Related questions

How do you declare the member of class static?

by the help of static keword in second statment.


How you declare class scoped variables and member functions?

To scope class members to the class (rather than to instances of the class), declare them as static members of the class. Static members are accessible even when no instances of the class exist. As such, static member functions do not have access to a 'this' pointer, unlike ordinary (nonstatic) member functions.


When do you declare a member as static in java?

You declare a member static whenever the member should be regarded as being local to the class rather than being local to objects of the class. Static members are shared by all instances of the class. Static methods of a class differ from ordinary members in that they do not have an implicit "this" reference, which means they can be invoked even when no instances of the class exist.


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 static data member and ordinary data member?

Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.


How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


What is members in java?

A member class is a class that is declared as a non-static member of a containing class. If a static member class is analogous to a class field or class method, a member class is analogous to an instance field or instance method.by k7


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 a static data linkage?

Static data members of a class in namespace scope have external linkage. Static data members follow the usual class access rules, except that they can be initialized in file scope. Static data members and their initializers can access other static private and protected members of their class. The initializer for a static data member is in the scope of the class declaring the member. A static data member can be of any type except for void or void qualified with const or volatile. The declaration of a static data member in the member list of a class is not a definition. The definition of a static data member is equivalent to an external variable definition. You must define the static member outside of the class declaration in namespace scope.


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.


How many static function can declare in one class?

It is dependent on the requirement of your usage of the Object of that class


What is the difference between static and non static class members in java?

Static data members are different from automatic ones in the way that their lifetime is equals to the lifetime of your program. Even if you have declared static members inside of function (class) other than main();