-2

All members must be explicitly specified as static, static class does not automatically make its members static. Static class can contain a collection of static methods.

The definition explains all i.e we need to explicitly give static for methods , members etc inside static class .

What i really didn't get here is if there is a rule like we can declare only static members inside a static class

why doesn't the developer of OOPL make it not mandate . so the compiler should understand(internally) i.e even if we declare a non-static method inside a static class it should understand the method as static (i.e static class can only have static methods) .

I got this doubt when i am working on interfaces see even in case of interfaces all interfaces members are public so we don't declare then public explicitly compiler will understand internally .

All i am looking is for a big WHY not for excuses(it should be like that , its in-build functionality).

super cool
  • 6,003
  • 2
  • 30
  • 63
  • 1
    there should be duplicate of this somewhere... this is a design decision so we can't answer WHY because we are not designers of the language. – Selman Genç Feb 16 '15 at 11:05
  • I like dav_i's answer. On your point about members being public implicitly, that is true but public is the default whereas static is never the default. – Jared Kells Feb 16 '15 at 11:07
  • `selman22` that was fact i'm looking why they done that . `dav_i` looks intresting . `downvote` sad , i didnt get why excatly . please do comment so i can correct myself (if there is possible chance) . i'm looking for something i dont know . cheers – super cool Feb 16 '15 at 12:23

1 Answers1

0

I imagine because a) it is easier to see and b) is consistent:

Easier to see

public static class WithoutStaticMembers
{
    public string GetString() // easy to miss that it is static
    {
        return "string";
    }
}

public static class WithStaticMembers
{
    public static string GetString() // clearly static
    {
        return "string";
    }
}

Consistant

public class NotStaticClass
{
    public static string GetString()
    {
        return "string";
    }
}

public static class StaticClass
{
    public static string GetString()
    {
        return "string";
    }
}
// ...
var s1 = NotStaticClass.GetString();
var s2 = StaticClass.GetString(); // consistent across both static class and not static
dav_i
  • 27,509
  • 17
  • 104
  • 136