Why do I need to instantiate the class I'm working in?

OK I am studying OOP at the moment, and there are a few things that I do not understand. For example, I have a class called PersonProgram that contains my main one. I have several methods and functions outside of my main method in the class. Why can't I name these methods and functions from the main one without creating a new class object? Since they are all in the same class, why doesn’t it make sense to just call them, like any other method, without PersonProgram p = new PersonProgram (); first?

+5
source share
6 answers

Why can't I call these methods and functions from the main one without creating a new class object?

Because they are not static

You need to create an instance to use it. Add a modifier so you can access them. For instance,

 private static getSomething() 

And then, name it from main as:

 getSomething() 

Also note what the almighty @JonSkeet said in the comments.

+4
source

Short answer: since the main method is static, it means that it belongs to the class, and not to a specific object. Static contexts cannot refer to a non-static context because it does not refer to a specific object.

But you could just make all your other variables and static methods too, and there the problem is solved, right?

No. Do not do that.

Long answer:

The main method is static , because none of your user defined application objects exist before it starts. After all, you cannot exist in a room before the room exists. However, the JVM needs an entry point to your program, so the main method should be free of all object references, hence static .

Java calls PersonProgram.main(args) same way your application starts. Now suppose you have determined that "Person" is in your PersonProgram class. Perhaps PersonProgram has a name, age, etc. But you don’t have any PersonProgram objects PersonProgram , since the main method knows what name or age it should work?

Here you can do something clearer: Create a new class called Application and put only your main method into it. Now try PersonProgram your PersonProgram instance PersonProgram . You cannot do this unless you first create a PersonProgram object.

You just started learning the concepts of OOP, and the idea of ​​putting the main method in class definitions can be confusing. This makes it easier to learn the code, but you should try to keep your main method separate from your class logic when you try to learn the principles of OOP.

+1
source

This is essentially because you can have multiple instances of the same class.

 Dog bigDog = new Dog(); Dog littleDog = new Dog(); 

Now, if you would like to know the name of your dog? Dog.GetName(); , right? What dog name will be back? You must report this to the compiler by calling the function on the class instance.

 bigDog.GetName(); 
0
source

If you want to process more than two people in your product, then you can do it,

 PersonProgram p1 = new PersonProgram();// Have p1.name="First person" PersonProgram p2 = new PersonProgram();// Have p2.name="Second person" 

and so on...

If you know that there is only one person, you can create a static class, and you will not need to create a new object and use it directly without creating an instance.

so OOP provides you with both options, choose the one that suits you.

0
source

Understand that the driving force behind creating Object Oriented Programming is to connect memory / data with operations that can be run on it. Thus, in OOP, in most cases, objects are designed to provide services based on the data contained in them, while static methods are fundamentally based on stateless processes.

0
source

Basic OOP rule:

A class makes all its non-personal, non-static members accessible only through its instance of the object.

This applies even when members are requested from the same class. Although you can also access private members of your class.

To use this.method() , you have to make the method static.

0
source

Source: https://habr.com/ru/post/1263221/


All Articles