What is static key word? [Resolved]

Posted by Ray.chayan under C# on 7/12/2010 | Views : 3588 | Status : [Member] | Replies : 9
What is the use of Static keyword. Why we want to access class member without creating it's object?
why I can't put a static method in a abstract class?




Responses

Posted by: Paurush83 on: 7/13/2010 [Member] Starter

Up
0
Down

Resolved
You can't call the private method from out side the class . so you need to Make a Method as public , now you can call it.

abstract class A

{

Public static void F()

{

Console.WriteLine("Static Method")

}

}

Now we can use this code in Main

A.F();

Paurush Sharma

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Paurush83 on: 7/12/2010 [Member] Starter

Up
0
Down
the use of the static variable is, if u want to share only one copy field to all instance field . While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.
We can be also defined Static methods in an abstract class. However, you cannot push a derived class to implement a static method. If you do like that, such a method would be useless. Static methods are invoked using type names, not instance variables. If I call BaseClass.MethodName, then BaseClass.MethodName will always be Called.

Paurush Sharma

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ray.chayan on: 7/13/2010 [Member] Starter

Up
0
Down
we can't use the Static method using abstract class name:

like

abstract class A

{
static F()
{
Console.WriteLine("Static Method")
}
}


Now we can't use this code in Main

A.F();

Why it isn't possible?


Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raja on: 7/13/2010 [Member] Starter

Up
0
Down
@Sivakumari: To answer your question, static keyword is used to define a variable whose only one instance can be used throughout the application. For example, you can define a Database connectionstring as static or any value that need to be used throughout the applicaiton as static.

Generally methods of the data access layer is defined as static and the same method is used thorughout the applicaiton from different different methods of business access layers.

Hope this will help. There are couple of more post related with static in this forum, just look at them and you should be able to know about it in details.

thank you

Regards,
Raja, USA

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 7/15/2010 [Member] Bronze

Up
0
Down
Adding the answer of Raja.

When we are using the single value in the application and we want to modify it and reused by other objects, we can declare it as static,

But if we are going to use as retrieval purpose only then it's better to use constant e.g. pi = 3.1416

Static Example: count of online user in the website.

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nav234 on: 7/16/2010 [Member] Starter

Up
0
Down
hi all ,

if static class can access methods without instializing object ,then why
to use Non-static class .?

when static class could not be inherited ,why and where we use static class ?

thanks in advance

Naveen...

S.Naveen...

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 7/17/2010 [Member] Bronze

Up
0
Down
It is not best practice to use the static class.
If you are going to use the same operation at various place, it is advisable to use the class. Because you will get the last value in static class method but new value in class object

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 7/17/2010 [Member] Bronze

Up
0
Down
It is not best practice to use the static class.
If you are going to use the same operation at various place, it is advisable to use the class. Because you will get the last value in static class method but new value in class object

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Maheshbabu on: 1/17/2014 [Member] Starter | Points: 25

Up
0
Down
Static Keyword:
The static keyword defines, that the class /property /method you declare as static does not require a previous instance of an object. In the other hand, a static method for example cannot use any instance method / instance property of the own object. Also, static defined things will be optimized by the compiler, for example that a static object is only written once in the memory and everything accesses to the same object.when we use static key word it means there is no need to create an instance of the object. static class including a static method can called class name.

To know complete information with live example . Visit below link to get complete code
http://www.dotnetchallengers.com/App_Folder/Content/Articles/C-Sharp/Static%20Keyword%20C-Sharp/Static_Keyword_C-Sharp.aspx?TutorialIds=77

Ray.chayan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response