Static field in class, how does it work

Posted on 2010-01-11
Medium Priority
Last Modified: 2012-05-08
Hello Experts :)

I am a C# newbie with some Delphi background.

I am struggling to understand the idea of static field in a class.


public class Jack
  public static int myField = 0;

then I can do something really strange to me...

I can assign value to a class, not even an instance of object of the class?

Jack.myField = 12;

1. How come can I use class like a variable which I do not even have to initialize?

2. How come can I change the value of ALL instances of object of the class Jacek by changing it somewhere in a code? It looks potentially very dangerous and far from a good practice of programming.

Any thoughts experts?

Question by:panJacek
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 21

Accepted Solution

silemone earned 2000 total points
ID: 26284932
1) That's the purpose of Static fields/ classes...to have the ability to call a function without having instantiate  the class...this is because that class is used throughout the app...meaning that every user uses it even though they are running their own app...
an example to illustrate this:  what if you wanted to know the count of all users on  your site...

if you instantiated a class, then it would always start at zero if you said something like this:
private int count = 0;

but if its static, the class is used by everyone...therefore the first person to log on would = 0,
but it would keep building for each additional user...

2)  answer 1 helps explain answer 2...you don't use this if you want all users variables to be user specific...but if you want to know about all users stats, you would use a static val
LVL 21

Expert Comment

ID: 26284982
2)  answer 1 helps explain question 2...you don't use this if you want all users variables to be user specific...
but if you want something like in the example i gave (user's logged in counter), then you would use that....

yes, as stated, you don't instantiate static classes as you want all users/classes to see the last value it held and not to start at it's default as it would if it was instantiated...
LVL 58

Expert Comment

ID: 26285002
in vb static is shared, may be a better wording ;)

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question