Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static keyword proper usage?

Tags:

c#

static

vb.net

Regarding the static keyword.

Up to this point, via my own research, I have a general idea of what the static keyword is, but I feel that all these different descriptions and details have only confused me more. At the moment, I really don't feel that I know how to properly use "static"; it seems to be used differently between C# and VB.NET and applies differently within the language depending on what you are using it for…

While reading the MSDN article Static (Visual Basic) , many questions arose, specifically when I read this sentence:

Normally, a local variable in a procedure ceases to exist as soon as the procedure terminates. A static variable remains in existence and retains its most recent value.

  1. Is the VB.NET version of static the same as C# or Java, is the concept the same for most languages?

  2. If static retain a value within a class, and we are able to access that certain member, function without instantiating the class, is this safe to use loosely? In other words, should we keep a close eye when using static’s within classes? They remind me of global variables for some reason. Maybe I’m just being ignorant here and simply need more practice to understand their purpose.

  3. What are good scenarios where using static benefits and promotes reusability of code?

like image 431
Dayan Avatar asked Jul 24 '12 14:07

Dayan


People also ask

What are the rules for static methods?

A static method can access static methods and variables as follows: A static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class.

When should static variables be used?

use static variables when : The value of the variable is independent of the objects (not unique for each object). E.g. number of students. Show activity on this post. Static variable: When you need something that will be used through out the application and every instance need to know the variable.

What is the use of static keyword give example in Java?

The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. The static variable gets memory only once in the class area at the time of class loading.


1 Answers

In C# the static keyword is the same as the shared keyword in VB.NET. Namely, from Static Classes and Static Class Members (C# Programming Guide):

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class.

In VB.NET static works very differently, as it applies to variables, not types.

I do not know if there is a C# equivalent for this behavior.

1) Is the VB.NET version of static the same as C# or Java, and is the concept the same for most languages?

It's different. shared is the same, though.

2) If static retain value within a class, and we are able to access that certain member, function without instantiating the class – Is this safe to use loosely? In other words, should we keep a close eye when using static’s within classes? They remind me of global variables for some reason, maybe I’m just being ignorant here and simply need more practice to understand their purpose.

Be very careful, they are global to the application domain.

3) What are good scenarios where using static benefits and promotes reusability of code?

I use static lists to cache sets of data that are used to populate dropdown lists so that I don't have to keep hitting SQL Server as one example.

like image 60
asawyer Avatar answered Oct 27 '22 09:10

asawyer