Instantiate class with Static method

Asked

Viewed 760 times

2

namespace Classes
{
    class Program
    {
        static void Main(string[] args)
        {
            Editora ed = new Editora();

            Program p = new Program();

            ed = p.EntradaDeDados();

            ed.InserirProvedorSql(ed);

        }

        public Editora EntradaDeDados() {
            Editora ed = new Editora();

            System.Console.WriteLine("Digite o nome da editora:");
            ed.Nome = System.Console.ReadLine();

            System.Console.WriteLine("Digite o E-MAIL da editora:");
            ed.Email = System.Console.ReadLine();

            ed.DataRegistro = DateTime.Now;

            return ed;
        }
    }
}

The class Program was created automatically next to the method static void Main. I made a method for data entry in this class. When I called this method in the method static void Main, I needed to instantiate the class Program within itself. I found it very strange. I realized that this happens when it is a method static. Why the method static forces us to instantiate the class itself?

I modified the method Main of static for public, but gave error when executing the project, said it does not contain a static Main. Why it is mandatory to have a method static? I also removed that "string[] args" I don’t know what it’s for.

  • If you want you can close or delete this question (I don’t know if you can delete it).

2 answers

8


There are some conceptual errors in the question and the comment of the PA in the other question is important, but in reverse.

The Program class was created automatically next to the Static void Main method

Learn how to program, don’t learn what some tool helps you. C# doesn’t create anything, so work with this idea.

I made a method for data entry in this class

In real codes we do not usually do this, each with its own responsibility.

When I went to call this method in the method static void Main, I needed to instantiate the class Program within itself

In the way that it was made needed even.

I realized this happens when it’s a method static. Why the Static method forces us to instantiate the class itself?

No, the problem is having a method that’s not static. Static methods are available for application, instance methods, which is the case of the EntradaDeDados(), require that an instance be created. So the question does not fit.

I modified the method Main of static for public

public and static are unrelated, so could have both, need not change.

The Main() need to be called by . NET, ie, need to be available for the application without needing to instantiate anything. So it must be static. The Main() in itself is not so special, you can determine the application’s entry point, but it always has to be static. The Main() is an interesting convention.

This has all been answered in Why the application entry point is a static method?. So technically the question is a duplicate, but there’s one small detail that’s not in the question that makes it interesting.

I just don’t know what to use it for static

The correct question should be: why not use static?

What do you get from not using static?

The gain is that you can create instances of that, you can generate an object based on that model. Actually not only can, requires, after all if a member is available to the instance you can only access it by the instance.

The simpler the better. Creating an instance without necessity makes no sense. It is true that in a real application this method would not even be there, but since it is, there is no reason why it should not be static. That’s the simple thing. Even at the right place would only make sense in another context, it would have to be a class that deals with the input of publisher data and created in a way that the instance would be more suitable than the static form.

But I can access methods that are not static within the static, need to make the instance of an object of the class that has this method

Power, it can, but for what?

One of the problems of the simplified example to learn is that it does not teach to structure an application. It’s great for learning simple, punctual concepts that many programmers ignore and that’s skipping a step, so most programmers can’t program, you’re going the right way, just don’t think you’re now learning to structure the entire application, This is a lot harder than people think.

Learn the punctual first and the whole after. Just do not lose sight that while you are learning the punctual is not structuring correctly. This is a common mistake in beginners, they see an example and they think that it is the right one when in fact it is just a simplification to show the point being taught. Nor will I get into the merit that some people can’t do and try to teach.

When you do something that requires an instance then we can help in this way, right now the solution to your problem is to make the method static.

Instantiate class or use public methods?

  • I didn’t do the method because it was the right thing to do or because it would improve the system, I want to understand why things. I think I’m not going to understand through this question, it’s better I look for the understanding of things, I was kind of flustered and I already wanted to ask why this and that. In fact, I had read some things, but I didn’t understand very well, I don’t know if I have a vocation for programming. And thank you for the answer.

2

We cannot modify the signature of the method Main, because it is special. It marks the starting point and automatically called when we run our program. See more here.

Static methods do not require us to instantiate the class itself.

What happens is that it is not possible to access instance members from within them, that is, any variable or method that is not static, cannot be accessed from a static method.

Otherwise, static members can be accessed normally from non-static members.

A less strange solution would be to make the method EntradaDeDados() in static also.

public static Editora EntradaDeDados() { /* ... */ }
  • But I can access methods that are not Static within Static, I need to do the instance of an object in the class that has this method. I just don’t know what to use Static for.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.