Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: static oops
Why static members are accessible outside without making instance?
Posted 10-Jul-11 21:28pm
Comments
CPallini at 11-Jul-11 4:30am
   
Because they are static.
:-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
SAKryukov at 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 at 11-Jul-11 4:09am
   
good answer my 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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?
  Permalink  
Comments
Uday P.Singh at 11-Jul-11 4:10am
   
good answer my 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
class A {
    internal void InstanceMethod(int value, string name) {/*...*/}
    //...
}
A a = new A(/*...*/);
a.InstanceMethod(3, "name");
 
Means this:
// 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
  Permalink  
v3
Comments
Uday P.Singh at 11-Jul-11 4:10am
   
good answer my 5!
SAKryukov at 11-Jul-11 4:18am
   
Thank you, Uday.
--SA
Manfred R. Bihy at 11-Jul-11 6:02am
   
Well explained! 5+
SAKryukov at 11-Jul-11 10:45am
   
Thank you, Manfred.
--SA
Espen Harlinn at 13-Jul-11 18:10pm
   
Nice reply, my 5
SAKryukov at 13-Jul-11 18:16pm
   
Thank you, Espen.
--SA
Silju MC at 23-Nov-11 23:40pm
   
Nice answer. My 5+
SAKryukov at 24-Nov-11 0:12am
   
Thank you,
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

That is the whole idea behind static.
Any static method can be accessible without using an instance.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,725
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,589
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 11 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100