Why Do Members of a Static Class Need to Be Declared as Static? Why Isn't It Just Implicit

Why do members of a static class need to be declared as static? Why isn't it just implicit?

I get asked questions like this all the time. Basically the question boils down to "when a fact about a declared member can be deduced by the compiler should the explicit declaration of that fact be (1) required, (2) optional, or (3) forbidden?"

There's no one easy answer. Each one has to be taken on a case-by-case basis. Putting "static" on a member of a static class is required. Putting "new" on a hiding, non-overriding method of a derived class is optional. Putting "static" on a const is forbidden.

Briefly considering your scenario, it seems bizarre to make it forbidden. You have a whole class full of methods marked "static". You decide to make the class static and that means you have to remove all the static modifiers? That's weird.

It seems bizarre to make it optional; suppose you have a static class and two methods, one marked static, one not. Since static is not normally the default, it seems natural to think that there is intended to be a difference between them. Making it optional seems to be potentially confusing.

That leaves making it required, as the least bad of the three options.

See http://blogs.msdn.com/b/ericlippert/archive/2010/06/10/don-t-repeat-yourself-consts-are-already-static.aspx for more thoughts on these sorts of problems.

C# Static Classes - A static class cannot have non-static members

Most likely for historic reasons. You are right that an automatic, implied static would be more in line with other parts of the language.

But static classes were new in C# 2.0, and the change had to be non-breaking.

If I create a static class in C#, will any methods inside be considered static as well, regardless of whether they're explicitly declared as static?

What's confusing me is that I was under the impression that when I declared the class as static, everything within the class should automatically be static as well

All members of a static class must indeed be static, but it is not happening automatically: you must explicitly declare all the members static. The purpose of declaring a class static is to let the compiler perform a check that all members are static, and to prevent any attempt at creating an instance of your static class.

Static Class and Variables

Yes, if you've defined your class with the static keyword, you also need to use static for all its members.

Are methods in static nested classes implicitly static?

A static class is just a regular class, in fact more so than a non-static class.

The difference between a static nested class and a top-level class is just access scoping: the static class can access private members of its enclosing class.

Once you get that cleared up, you won't need to ask the question that you are asking here.

Why can't inner classes declare static members?

Basically just an arbitrary decision. there's no reason it couldn't be supported, but there is also not really any good reason to support it. just declare the static field in the outer class.

also, that quote may not be entirely correct: i believe you can declare a static serialVersionUID in an inner class.



Related Topics



Leave a reply



Submit