Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why static members are accessible outside without making instance?
Posted
Comments
CPallini 11-Jul-11 4:30am    
Because they are static.
:-)

It's by design. Static methods have their merit in cases when functionality is independant to instance variables. Also it's important for program entry points like main in applications.

Cheers!

—MRB
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jul-11 3:46am    
Good point about Main, my 5. This is just a convenient notation. Before a first class is created, what would be a first method to be called? (Strictly speaking, it is not 100% of cases, some methods can run before main.)
This is not the only possible architecture for entry points though.


OK, please see my answer for a very fundamental explanation.
--SA
Uday P.Singh 11-Jul-11 4:09am    
good answer my 5!
It's a "lightbulb" moment, and difficult to explain without pictures, (and seeing your face), but...

Lets talk about cars.

How many wheels has a car?
What color is a car?
What fuel does a car use?

The first question is easy: four.
The other two are harder: "a car" does not have a color, or use a fuel type. "this car" does. "that car" does. But "a car" doesn't, any more than you would drive "a car" despite learning to drive "a car". Instead you need an instance of a car to do any of those things. You drive "your car", "your car" is green, and uses petrol.
"My car" is a different instance : it could be red, and use diesel.

But, all of them - "this car", "that car", "your car" and "my car" - share common features: they all have four wheels. This is a static property - it is not specific to any single instance of a car, but is general and shared by all instances.

In software it is the same: a static variable or method is not specific to any instance, it is the same for them all, so you do not need to specify an instance in order to use it.

Does that make sense?
 
Share this answer
 
Comments
Uday P.Singh 11-Jul-11 4:10am    
good answer my 5!
Right question should be: what makes a non-static method able to access the instance?

This is because there is nothing special about static methods. They are completely equivalent to old good functions existing before OOP. They receive only those parameters which are explicitly listed in their signature and can work only with these parameters. The can call other static methods and properties (all properties are actually called) and use static variables provided there are sufficient access to them. That cannot access anything non-static beyond their parameters. Of course, some parameters can be referenced to instanced of classes or structures; in this and only in this way a static method can access non-static members of those instances.

In contrast, a non-static method gets access to some instance. In this way, it needs instance to be called. Where it comes from? Actually, all non-static methods (also called instance methods) have additional hidden parameter "this" user to reference the instance.

In effect, something like
C#
class A {
    internal void InstanceMethod(int value, string name) {/*...*/}
    //...
}
A a = new A(/*...*/);
a.InstanceMethod(3, "name");


Means this:
C#
// Pseudo-code, not code!

A a = new A(/*...*/);
A.InstanceMethod(a, 3, "name);
// when implementation uses "this",
// it is equal to the reference of the parameter 'a' passed in this call


Got the idea?

—SA
 
Share this answer
 
v3
Comments
Uday P.Singh 11-Jul-11 4:10am    
good answer my 5!
Sergey Alexandrovich Kryukov 11-Jul-11 4:18am    
Thank you, Uday.
--SA
Manfred Rudolf Bihy 11-Jul-11 6:02am    
Well explained! 5+
Sergey Alexandrovich Kryukov 11-Jul-11 10:45am    
Thank you, Manfred.
--SA
Espen Harlinn 13-Jul-11 18:10pm    
Nice reply, my 5
That is the whole idea behind static.
Any static method can be accessible without using an instance.
 
Share this answer
 
Further to the other answers, static members belong to the class and not the instance. Because they are class related, there is no requirement for an instance to be created. Also, as they do not belong to any specific instance, you cannot reference the this pseudo-variable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900