1

Just curious,

If I have a static class, do I also have to define the variable as static since I already said the class would be static?

This is more out of curiosity, in my static class currently I also have my variables defined as static;however, I was wondering if this was actually necessary.

I am using C#.

Thanks

crashmstr
  • 28,043
  • 9
  • 61
  • 79
CodeMusic
  • 87
  • 10
  • 3
    Static has different meanings in different languages!!! Which language is you taking about> – Devavrata Jan 12 '15 at 19:31
  • There are language tags for a reason. Added C# tag. – crashmstr Jan 12 '15 at 19:47
  • C#. I meant more, if I am defining the class as static (in this case, saying that I don't have an instance of it)... then variable-wise would there be a difference. Like by defining the class as static, wouldnt that imply the contained variables were static. – CodeMusic Jan 12 '15 at 19:48
  • in C#, to access a variable in a static class it must be declared static otherwise it wont be seen. a static, in a way, is like a singleton class. it is not an instance of a class but rather just itself... you cannot create instances of static classes. so yes, it is required. – Ahmed ilyas Jan 12 '15 at 19:49
  • 1
    possible duplicate of [C#.NET - Why do members of a static class need to be declared as static? Why isn't it just implicit?](http://stackoverflow.com/questions/6005109/c-net-why-do-members-of-a-static-class-need-to-be-declared-as-static-why-isn) – CodeCaster Jan 12 '15 at 19:53

4 Answers4

1

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

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • ok, I am guess the one exception to this is CONST. The reason I say this is because I had a constant defined and by habit I added static (like all my other variables) but it of course didnt like this (I mean it makes sense with a constant and all) – CodeMusic Jan 12 '15 at 19:59
  • 1
    By _members_, I just mean properties, methods, and fields. Constants are implicitly static, so they don't need the `static` keyword and, as you discovered, it is forbidden for them. – Mark Cidade Jan 12 '15 at 20:14
1

Yes. From MSDN

The following list provides the main features of a static class:

  • Contains only static members.
  • Cannot be instantiated.
  • Is sealed.
  • Cannot contain Instance Constructors.
Yuval Peled
  • 4,988
  • 8
  • 30
  • 36
1

Here is a quick way to test. Copy the following code and paste it to your IDE (Visual Studio). Then uncomment each of the two commented lines, one at a time.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StaticTest
{
    static class Program
    {
        /**
         * Uncomment one line at a time and compile program.
         **/

        //public int NonStaticVariable = 0;
        //public static int StaticVariable = 0;

        static void Main(string[] args)
        {
        }
    }
}

You will notice that for the non-static variable, the complain:

'StaticTest.Program.NonStaticVariable': cannot declare instance members in a static class

This is because, in .NET, a static class can contain only static members. If you are looking to read further into this, follow this link: http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

GMalla
  • 191
  • 1
  • 7
0

If you are using Java you can't use non-static variables from a static context.

And also it depends on which language you are using ( you should have told us that).

But have a look here:

Java: Non-static variable cannot be referenced from a static context

C++: The static keyword and its various uses in C++

hope it helps :)

Community
  • 1
  • 1
Johan
  • 475
  • 4
  • 17