regarding concept of anything static ...like method, member etc
i have one doubt in mind that....
1) we can call & access any static method or variable from any non static method but from static method we can not access any non static method or variable.....why.
2) but we can create a instance of any class from static method.
please tell me the reason to understand better answer of my question.
class Class1
{
st开发者_如何学Goatic int x=0;
int w = 0;
private static Class2 test()
{
w = 88; // give error because w is not a static member.
test1(); // give error because test1() is not a static function.
Class2 z = new Class2(); // here i am creating instance of class2
return z;
}
private int test1()
{
x = 9;
return x;
}
}
class Class2
{
}
thanks
Have a look at the MSDN article on static classes and a basic tutorial on static classes.
In answer to your direct question, static methods are not part of an instantiated object (this
) and therefore cannot access any methods/fields that require an instantiated object.
In your example, test1()
could just as easily be operating on the field w
. In that case an external caller could call Class1.test()
without any of the fields of Class1 having been created. If Class1 had been instantiated, its fields have been created and therefore can be operated over.
I found this Stackoverflow Post which is worth reading for some more background on static.
1) we can call & access any static method or variable from any non static method but from static method we can not access any non static method or variable.....why.
The reason you cannot access the non-static variable or method from a static method call is because they haven't been created and more importantly don't exist in memory. A static method does not call the constructor of the non-static class it belongs to.
2) but we can create a instance of any class from static method.
Anything declared and instantiated within a static method is still allocating memory (remember scope) so they are usable but only within that static method. For eg, you couldn't assign a member of he non-static class from within a static method.
This link might help explain it in more detail.
Static keyword demystified
To understand the reason why you'd first have to understand the difference between a class
and an actual object
.
I can define a class like the following:
public class Car
{
public string Brand {get; set;}
}
What i will be telling the compiler by this is what a Car
would look like if i where to create such a thing. No Car
is created at this point but the computer has an understanding of what a car is. We have given it a blueprint. This is called a class
Next i can go and actually create a car:
Car myCar = new Car();
myCar.Brand = "Ford";
Car otherCar = new Car();
otherCar.Brand = "BMW";
Now myCar
contains an actual object
and i told the computer it is a car. I also created another car in otherCar
. These are both individual objects. and both have their own Name
belonging to the object.
So now we have 2 objects but still only 1 class.
Now sometimes the need may arise for properties
of methods
that have some link to the concept Car
but don't require a specific Car
to work on. in C# we can define such members using the static
keyword.
We can add the following method
to our class:
public static Car GetFirstCarEverBuild();
To actually find the first car that was ever build we do not need access to a specific car. we will try to find a car here but we don't start out with one. so we mark it static
. this also means that we have to access to Brand
of the car since we have no object
to get it from. The case might even be that no Car
at all was ever created in wich case the method can just return Null
. No car has ever existed and you don't need one to find that out.
the same goes for static fields and properties:
public static Car firstCar;
This field can store 1 car and 1 only. no matter how many cars you create. it's value is tied to the class and not the object. So if myCar
will store a value in this field then otherCar
can read this same value.
Consider the following example to sum it all up:
public class Car
{
public static Car firstCar;
public Car()
{
if (firstCar == null)
firstCar = this;
}
public string Brand {get; set;}
public static Car GetFirstCarEverBuild()
{
return firstCar;
}
}
It's the same reason you can't invoke a method on a null object - non static methods/fields/properties require an instance of the type to call against.
Imagine you have this class:
Class Product {
static int getTotalWeightOfAllProducts()
int getWeight()
}
The reason you are never interested in calling an instance method from a static method is because it doesn't make sense. Let's say you have 2 products.
computer and xbox. Like this
var xbox = new Product()
var computer = new Product()
xbox.getWeight() // would return the xbox weight
computer.getWeight() // would return the computer weight
// But calling getWeight like this doesn't make sense
Product.getWeight() //because weight is product specific thing to do
//However getting all products total weight would make sense to return through a static call like this:
Product.getTotalWeightOfAllProducts() // because "getting all products total weight" isn't a product specific thing to do. Therefore it should reside in a static method.
Basically instance methods are "Product"-specific behavior and static methods are shared "Product" functionality. Don't know if this makes sense.
getTotalWeightOfAllProducts
doesn't even know that the xbox
and computer
instances exists and therefore calling getWeight() from within getTotalWeightOfAllProducts
doesn't makes sense. Because even if it was possible, what would it return? The weight of the xbox or the weight of the computer?
You have to gain a basic understanding on static and instance members.
精彩评论