Hi, i know my following question may seem rather stupid but im having some problems understanding the static concept especially in the following code and id appreciate some help.

4: using System;
5:
6: public class myClass
7: {
8: static public int sctr = 0;
9: public int ctr = 0;
10:
11: public void routine()
12: {
13: Console.WriteLine(“In the routine - ctr = {0} / sctr = {1}\n”,
14: ctr, sctr );
15: }
16:
17: public myClass()
18: {
19: ctr++;
20: sctr++;
21: Console.WriteLine(“In Constructor- ctr = {0} / sctr = {1}\n”,
22: ctr, sctr );
23: }
24: }
25:
26: class TestApp
27: {
28: public static void Main()
29: {
30: Console.WriteLine(“Start of Main method...”);
31:
32: Console.WriteLine(“Creating first object...”);
33: myClass first = new myClass();
34: Console.WriteLine(“Creating second object...”);
35: myClass second = new myClass();

The output is as follows:

Start of Main method...
Creating first object...
In Constructor- ctr = 1 / sctr = 1
Creating second object...
In Constructor- ctr = 1 / sctr = 2


From what i have understood, the SCTR is a static and is therefore only initialized only at the begining of the program and then never again hence why it can increment. CTR on the other hand is public so Line 9 which sets it to 0 each time the myclass object is accessed.

if the above is right, could someone please clarify why there is a static in line 28 as the description i found in my c# book wasnt particularly useful.

Recommended Answers

All 5 Replies

Welcome Firefly1985.

Your source code must be surrounded by bb code tags.
Read this article -
How to use bb code tags?

Your class definition consists two fields:

static public int sctr = 0;    // static field 
   public int ctr = 0;               // instance field

A static fields (variable) comes into existance (or created or allocated) while execution of an application begins and it is removed (deallocated) from memory then an application ends.
(So - in c,c++ it is called global or external)

An instance field or variable of a class comes into existance when a new instance (object) of that class is created and ceases to exist when there is no reference to that object (object remove from the memory or garbage collected).

Sorry about missing the brackets around the code + thanks for replying to my initial question. Regarding my latter question, why is static required in line 28?

Thanks again

We want to create a such variable whose value is stored, changed, and read throught the life of an application. For example - You are developing a student class with roll,name, and schoolname fields to store student data of your 50 class roommates. If roll,name,and schoolname are instance members then 50 copy of roll,name,and schoolname will be prepared.

Consider - all 50 students have common school - so in this case you have to assign 50 roll, 50 names and 50 times same schoolname.

If we choose schoolname as a static then the single copy of school will be prepared and it is used/shared by all instances.

Yeah ok i think i get it now, thanks very much for taking the time to reply to my questions, much appreciated.

Thanks.
Mark this thread as Solved if you got suitable answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.