answersLogoWhite

0


Best Answer

Static members are members of the class, not an instance of the class.

All instances of a class have access to all their static members. Derived classes have access to both protected and public statics, while public statics are fully accessible.

Think of static members as being global variables or functions, but scoped to the class rather than being globally accessible (and therefore publicly accessible).

Consider the following example. A private static member variable, count, is declared. Note that it must be initialised immediately after the class declaration, but outside of a code block. Were it declared public it could be initialised in a code block, but the class needs to maintain control over it, thus it is completely hidden from all outside influence.

The two constructors increment count while the destructor decrements count. Thus count maintains a running total of all the instances of the class (much like a reference counter). Note that all instances of the class have direct access to the static member. Note also that if other parametrised constructors are declared, each must increment countindependently.

The public static accessor method, GetCount(), returns the current value of count (returning by value, not by reference). There is no equivalent set mutator, thus countis a read-only variable -- it cannot be altered from outside of the class.

#include

class MyClass{

public:

MyClass(){ ++count; }

MyClass(const MyClass & copy){ ++count; }

~MyClass(){ --count; }

public:

static unsigned int GetCount(){ return( count ); }

private:

static unsigned int count;

};

// Initialise the static member:

unsigned int MyClass::count = 0;

void PrintCount()

{

printf( "There %s currently %u instance%s of MyClass.\n",

MyClass::GetCount() 1 ? "" : "s" );

}

int main()

{

MyClass * myClass[10];

// At this point there are no instances.

PrintCount();

printf( "\nCreating instances of MyClass...\n" );

for( int i=0; i<10; ++i )

{

myClass[i] = new MyClass();

PrintCount();

}

printf( "\nDestroying instances of MyClass...\n" );

for( int i=0; i<10; ++i )

{

delete( myClass[i] );

PrintCount();

}

return( 0 );

}

User Avatar

Wiki User

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

Wiki User

12y ago

When a method is static it can be called without instantiating the class of which it belongs. This means that it is possible to call the method before the class it belongs to has been created.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why objects are not used to acces the static class members?
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 is the type of class for which object cannot be created?

A class that is declared as "final" cannot be inherited.


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();


What kind of method is applied to all objects of a class rather than individual?

A class static method can be applied to all objects of a 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.

Related questions

What is static class?

A static class is a class where all the members are declared static.


Are the methods or members of static class static?

A static class may or may not have static members. Adding the static keyword to the class definition won't change this. Note that an inner class which is not static may not contain static members (unless those members are also declared final).


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.


How is dynamic initialization of objects is achieved?

by creating class as an static class


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 is the type of class for which object cannot be created?

A class that is declared as "final" cannot be inherited.


How do you access the static variable and static methods of a class?

In java we access static variables and static methods without creating objects. i.e.,we can access directly by using classname we can also access static variables and static methods by using objects which are created by using class where the static variables and static methods are available


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();


What kind of method is applied to all objects of a class rather than individual?

A class static method can be applied to all objects of a class.


What do you understand by static members?

Static members are local to the class in which they are declared. Unlike instance members (non-static members), they are not associated with any one instance of the class, and are available (access permitting) even when no instance of the class exists. They can be thought of as being global functions and variables, but with a much more refined scope.


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.


Why is it not advisable to use this pointer with static members?

It is not inadvisable, it is impossible. Static member methods do not have access to a this pointer since they are not associated with any instance. Static members are scoped to the class, not to an object (an instance of the class). Only instance members have access to the this pointer.