answersLogoWhite

0


Best Answer

Static data members are local to the class, not to any instance of the class. That is, you do not need to instantiate an object of the class to access them. They are shared variables, not unlike global variables, the only difference being that that can also be hidden behind static member accessors and/or mutators (get and set methods) of the class. However, a public static data member is a global variable in all but name.

Since they do not belong to any instance of the class, they must be initialised outside of the class, and outside of any other code blocks. In other words, they are initialised at compile time and are therefore available at runtime, and can therefore be accessed from the main function if a public interface is implemented or the member is declared public. If it is declared private, the variable is treated as a shared variable that is only accessible to all static members of the class, to all instances of the class and to all friends of the class. If declared protected, it is also accessible to derived classes.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why a static data member can be accessed through main?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


What are non static members in Java?

Local variables (on the stack) or dynamically allocated variables (in the heap) are nonstatic variables. Static variables, constants and globals are all allocated in the program's data segment.


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


Explain static data member with the help of example?

Yes. Static data members are local to the class in which they are declared. Thus all instances of the class share the same variables (unlike non-static data members where each instance of the class has its own set of variables). Moreover, since static data members do not belong to any instance of the class, they are accessible without the need to instantiate an instance of the class, and like all other static variables, remain in scope for the entire duration the program is running. Also, as with all other static variables, they must be initialised at compile time from outside of the class declaration. Usually this is done from the class CPP file.Static member functions are similar to static data members in that they are local to the class, rather than to an instance of the class. Since they do not belong to any instance of the class, they do not inherit an implicit this pointer. As a result, they are accessible without the need to instantiate an instance of the class and will remain in scope for the entire duration the program is running.It is not unusual for a class to have both static data members and static member functions. They can be likened to global variables and global methods, but scoped to the class. However, their visibility can be restricted by the access specifiers enforced upon them (public, protected or private). Although static member functions cannot access instance methods and instance variables (unless an instance is physically passed to them as an argument) they have unrestricted access to the static data members of the class, as do all instances of the class and friends of the class.A classic example of static member functions and static data members in the same class is when one needs to maintain a count of all instances of a class. All the class constructors must increment the static counter while the destructor must decrement it. A static member function such as GetCount() can then report the number of instances currently instantiated, even when there are no instances.There are many other uses, but the golden rule is that they must be related to the class in which they are declared. If their purpose is simply to provide global functionality then declare them as such, or (better) limit their scope by declaring them in a separate class specifically for that purpose with private constructors to prevent any instances from being created (since none would be required).


When do you declare a member of a class static?

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

Related questions

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.


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.


When Static function can work with static data member only?

Always.


What are non static members in Java?

Local variables (on the stack) or dynamically allocated variables (in the heap) are nonstatic variables. Static variables, constants and globals are all allocated in the program's data segment.


How is memory allocated to the private-data member function when an object is not created during the calling process?

A class can have both static and non-static data. Static data is local to the class while non-static data is local to each object of the class. It makes no difference whether static data is global, local to a file, local to a function or local to a class (whether public, protected or private), all static data is allocated within the program's data segment along with all constants. As such they are allocated at compile time.


Why static data members are not initialized in member function?

Static data members are local to the class in which they are declared. That is, they are shared amongst all instances of the class, unlike instance variables where each instance has its own set of variables. In addition, static data members are also accessible to static member functions, even when no instances of the class actually exist. So if static data members are accessible even when no instance exists, how are we to initialise them? A member function is no use because that would require an instance. And a static member function isn't an option either because then the onus is upon the user to ensure that the method is called BEFORE any instances are created, which completely destroys the encapsulation of the class (not to mention the fact a static member function would require a local static variable in order to determine if it had already been called or not). The simplest solution is to initialise all static data members from outside of the class declaration. Typically we do this from within the class CPP file however we can also do it in the header file, so long as it's not declared within the class declaration. As with all static variables, the initialisation statement is executed at compile time, thus ensuring the member is fully initialised at runtime.


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 is the type of class for which objects can not be created except the static member?

Abstract data types or abstract base classes.


What does Data Accessed mean?

Data accessed refers to the act of retrieving, viewing, or using data stored in a database or other electronic storage system. This can involve running queries, performing searches, or simply viewing the content of a file or database record. Accessing data is a fundamental part of using and analyzing information in various digital platforms.


How data are accessed from direct accessed storage?

the answer is you must times by five then you how who livews


How can static elec affect your PC?

Static electricity can affect your PC by erasing/corrupting your stored data. The most common way of damaging a USB storage device is through common static electricity.


Explain static data member with the help of example?

Yes. Static data members are local to the class in which they are declared. Thus all instances of the class share the same variables (unlike non-static data members where each instance of the class has its own set of variables). Moreover, since static data members do not belong to any instance of the class, they are accessible without the need to instantiate an instance of the class, and like all other static variables, remain in scope for the entire duration the program is running. Also, as with all other static variables, they must be initialised at compile time from outside of the class declaration. Usually this is done from the class CPP file.Static member functions are similar to static data members in that they are local to the class, rather than to an instance of the class. Since they do not belong to any instance of the class, they do not inherit an implicit this pointer. As a result, they are accessible without the need to instantiate an instance of the class and will remain in scope for the entire duration the program is running.It is not unusual for a class to have both static data members and static member functions. They can be likened to global variables and global methods, but scoped to the class. However, their visibility can be restricted by the access specifiers enforced upon them (public, protected or private). Although static member functions cannot access instance methods and instance variables (unless an instance is physically passed to them as an argument) they have unrestricted access to the static data members of the class, as do all instances of the class and friends of the class.A classic example of static member functions and static data members in the same class is when one needs to maintain a count of all instances of a class. All the class constructors must increment the static counter while the destructor must decrement it. A static member function such as GetCount() can then report the number of instances currently instantiated, even when there are no instances.There are many other uses, but the golden rule is that they must be related to the class in which they are declared. If their purpose is simply to provide global functionality then declare them as such, or (better) limit their scope by declaring them in a separate class specifically for that purpose with private constructors to prevent any instances from being created (since none would be required).