Matt
Matt

Reputation: 26971

What is the point of a static method in a non-static class?

I have trouble understanding the underlying errors with the code below:

class myClass
{
    public void print(string mess)
    {
        Console.WriteLine(mess);
    }
}

class myOtherClass
{
    public static void print(string mess)
    {
        Console.WriteLine(mess);
    }
}

public static class Test
{
    public static void Main()
    {
        myClass mc = new myClass();
        mc.print("hello");

        myOtherClass moc = new myOtherClass();
        moc.print("vhhhat?"); 
       //This says I can't access static method in non static context, but am I not?

    }
}

I can't ever think of a reason why one would declare a static method in a non-static class, so why will .NET not throw an exception error.

Furthermore,

moc.print("vhhhat?");

This will say I can't access static method in non static context but Test and main are static, what is it referring to ?

Upvotes: 45

Views: 61770

Answers (6)

Shruti Sehgal
Shruti Sehgal

Reputation: 89

The correct program would be:-

class myClass
{
    public void print(string mess)
    {
        Console.WriteLine(mess);
    }
}

class myOtherClass
{
    public static void print(string mess)
    {
        Console.WriteLine(mess);
    }
    public void printMe(string mess)
    {
        Console.WriteLine(mess);
    }
}

public static class Test
{
    public static void Main()
    {
        myClass mc = new myClass();
        mc.print("hello");

        myOtherClass moc = new myOtherClass();
        myOtherClass.print("vhhhat?");
        moc.printMe("test me");


    }
}

Upvotes: 3

Sean
Sean

Reputation: 4470

First, the error:

moc.print("vhhhat?");

Is trying to call a static method on an instance of the class (i.e. a non-static context). To properly call print(), you would do

myOtherClass.print("vhhhat?");

For the first question, there are many reasons to have static methods in a non-static class. Basically, if there is an operation associated with the class, but not with any particular instance of the class, it should be a static method. For example, String.Format() (or any of the String static methods) should not operate on string instances, but they should be associated with the String class. Therefore they are static.

Upvotes: 25

ars
ars

Reputation: 123468

Sometime the "objective" of the function is particular to the class rather than the object (instance of the class).

For example, a factory method:

SomeClass obj = SomeClass.CreateInstance();

This is more apparent when the language has metaprogramming facilities that allow operations on classes. In Python, this distinction is made more explicit by convention: the first parameter passed to a function is either named something like "cls" or "self" and indicates that the function might operate on the class (when it's a "class method") or the instance (the type you're more used to, when it's an instance method).

Upvotes: 4

Reed Copsey
Reed Copsey

Reputation: 564333

This will say I can't access static method in non static context but Test and main are static, what is it referring to ?

This is referring to the fact that you're referencing a static method (myOtherClass.print) using an instance (moc). You would have to rework this to be:

myOtherClass.print("vhhhat?");

That will compile correctly.

Static methods are methods that work on the class itself, not a specific instance of the class. This has many uses - one example is for implementing the Factory method pattern.

Upvotes: 14

EFraim
EFraim

Reputation: 13028

When you are calling a method on an object instance you are calling it in a non-static context. It is of no importance in which method you do this.

Upvotes: 0

AlbertoPL
AlbertoPL

Reputation: 11509

A static class means that you cannot use it in a non-static context, meaning that you cannot have an object instantiation of that class and call the method. If you wanted to use your print method you would have to do:

myOtherClass.print("vhhhat?");

This is not static, as you created an instantiation of the class called moc:

myOtherClass moc = new myOtherClass();

Upvotes: 31

Related Questions