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

5
  • 3
    Static has different meanings in different languages!!! Which language is you taking about> Commented Jan 12, 2015 at 19:31
  • There are language tags for a reason. Added C# tag. Commented Jan 12, 2015 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. Commented Jan 12, 2015 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. Commented Jan 12, 2015 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? Commented Jan 12, 2015 at 19:53

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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)
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.
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.

Comments

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

Comments

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.