+ 2
What does 'static' mean? And, what does it do?
Can you please tell me what does static mean and what it does? I'm still learning and I didn't undertand that. Thanks :)
5 Answers
+ 7
Normally, if you have
class Person
{
int age;
string name;
public void SayHi()
{
Console.WriteLine("Hi");
}
}
You can then declare an object and use the class methods against that object:
Person customer = new Person;
customer.SayHi();
//outputs "Hi"
But if you have a static class, you access the methods and members of the class itself, and you cannot create (or "instantiate") objects using that class:
static class Person
{
int age;
string name;
public void SayHi()
{
Console.WriteLine("Hi");
}
}
Person.SayHi()
// outputs "Hi"
Person customer = new Person;
// generates an error!
+ 4
Static is a variable and a function that has a scope and a lifetime associated with the container program.
+ 3
Static makes the members of the clases belog to inself, insteand of beloning to individual object. For example if you define a class Animal with the variable count, there alwas be only one no matter how many animals objets you instantiens, otherwise you goin to create a variable for each objet you crate with this class.
+ 1
if you are set the value same for all you declare that variable as static
+ 1
static is a keyword if the value is same for all
then declare a variable as static
if you create the method as static then we cannot create object you can directly access by using class name