FAQ: Static Members - Static Fields and Properties

This community-built FAQ covers the “Static Fields and Properties” exercise from the lesson “Static Members”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Static Fields and Properties

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I really din’t get this course. I don’t understand what static means.
I did:

private static int forestsCreated;
public static int ForestsCreated
    {
      get { return forestsCreated; }
      private set { forestsCreated = value; }
    }

I get:
tests/test.cs(15,32): error CS0246: The type or namespace name ‘Forest’ could not be found (are you missing a using directive or an assembly reference?) [/home/ccuser/workspace/csharp-static-members-static-fields-and-properties/StaticMembers.csproj]

What is wrong?

What file are you running the code in? I can’t see a test.cs file. Did you manually create this? If so, it does not have the Forest namespace set at the top of the file. Let me know if that resolved it.

Soooooo what does Static mean? What does it do? And what’s its purpose?

I’m not too sure what’s going on as the lesson’s text is ‘non-beginner friendly’, I want to known what static does and when we should use it. But my PRIMATIVE brain can’t understand the ‘too-well-put-text’ written here for only the sophisticated.

Sorry. Please help.

Thanks for your help. I didn’t see it until today. What I did was try clicking on the “restart exercice” button, then I did the exercise the same as before, but it went alright this time. I’m not sure where the problem came from.
So I guess what you write from previous exercises is kept for the following ones?

Ya. I think it is kept as long as you save the previous work is kept. Not sure about the restart exercise though.

I’m unsure why we are using the same setter here even though we are incrementing ForestsCreated.

private set {forestsCreated = value;}

How does using this setter allow incrementation at all?

We have a private static property

public static int ForestsCreated {
    get { return forestsCreated; }
    private set { forestsCreated = value; }
}

In the constructor, we have the statement

ForestsCreated++;

This is shorthand for

ForestsCreated = ForestsCreated + 1;

In the above statement, we are going to evaluate the expression to the right of the assignment operator = first. When we try to access the value for ForestsCreated, this will call the get method of the ForestsCreated property. Whatever current value is assigned to the forestsCreated field will be returned and then the expression ForestsCreated + 1 will be evaluated. This will result in some number.
When we try to assign this number to ForestsCreated (i.e. when we try to assign this number to the variable to the left of the = operartor), this will call the set method of the ForestsCreated property. In the setter we have the statement,

forestsCreated = value;

The number that we calculated earlier (by adding 1 to the previous value) will be treated as the value and will be assigned to the forestsCreated field.

Every time a new instance is created, the constructor will be called. Within the constructor, the statement ForestsCreated++; will end up incrementing the private static field forestsCreated as per the above process.

1 Like