Class method versus core methods

I tried to find what I thought I was looking for, but in the end, I did not see anything that related to what I was asking. So sorry if this is a frequently asked question. I just did not know what I was looking for :)

I am now in my first class in Java programming. I am starting to realize that I am rather confused by the concept of creating a class using methods and the main method with the methods below.

What exactly are the differences here? How is it better than the other way around? What situations call the method under the main, not the class?

I would really appreciate it if someone could demystify this. This would help me understand my final project - the base game Craps, which will contain the Shooter class, the Die class and the Craps class (which apparently contains the main one).

+6
source share
5 answers

This is mainly a matter of separation. You create classes for reuse. If everything were in the main class, then your code would not be maintainable. The main class is used as a starting point for your application. If you did everything in the main class, you would not be able to take advantage of everything that offers object-oriented programming. You will learn more about this when you enter the course. But in principle, each of the classes that you create will be responsible. Even the main class that is tasked with running the program.

Good luck in your class. Perhaps this will give you an advantage in the casino.

+4
source

You have no "basic" methods. All methods are a class method, the only difference is that the "static" methods (vg, main) do not need to create a new object (via the "new" one) to use them.

In other words:

public class MyClass { public static void myStaticMethod() { } public void myInstanceMethod() { } } 

You can do

 MyClass.myStaticMethod() 

but to use myInstanceMethod you have to create an object

 (new MyClass).myInstanceMethod; 

Usually you do the last as

 MyClass myObject = new MyClass(); myObject.myInstanceMethod(); 

Please note that you can also do

 myObject.myStaticMethod(); 

but this is exactly the same as doing

 myClass.myStaticMethod(); 

and the first method is considered bad style and usually causes a compiler warning.


@Miranda, because only with the help of static methods you lose the whole object-oriented part, and you just end using Java, since you would use regular C.

In an object, you have both state and methods. In a class, you save both the state of the object and the methods. For example, you can usually create a class "Card", create a card "New card" ("K", "Sheet") "and have methods for processing it (" uncoverCard () ").

As soon as you have a reference to the object you want, you use its methods, and you know that you affect only this object, and that you are using the correct version of the method (because you are using the method specific to this very class).

Switching to OO programming from procedural programming may seem tricky at the beginning. Keep trying, looking at the training code and asking for advice when necessary, and you will soon realize this.

+4
source

The entry point for a Java application is the main method:

 public static void main(String[] args){ // Do stuff here } 

This method differs from others in that it is:

  • The entry point of your application, so this will be the first method called and basic initialization should be done here.
  • This is a static method that makes it available without instantiating a class that holds it.

This static -thing is best illustrated as follows:

 public class Test{ public int addOne(int number){ return number++; } public static int drawOne(int number){ return number--; } } // Create our Int: int value = 12; // This will work because the method // is a static one: System.out.println( Test.drawOne(value) ); // Outputs 11 // This won't work because it a non- // static method: System.out.println( Test.addOne(value) ); // Error // But this will work: Test ourTest = new Test(); System.out.println( ourTest.addOne(value) ); // Outputs 13 

An application written in Java usually has only one main method, which declares the point at which the application first starts.

If you want to do things with variables, objects, etc., you will want to create your own methods / classes, but they will not be the main methods.

+3
source

First of all, a method cannot have another method inside it. Each method must be part of a class. The difference between the main method (actually the main method you are talking about) and any other method is that the program starts with the main method.

A method is a functionality or set of commands that have been grouped together. A class is a set of methods and variables that define an entity (for example, Shooter, Die).

So, you will have the Die class, which will contain methods that will contain only special methods, the Shooter class with methods specific to the Shooter and the Craps class, where the game starts, which uses the Die and Shooter methods to end the game.

+2
source

I am sure that some of the other guys here will be able to explain this much better than I can, but I will give him a chance.

So, as you know, your main method is the entry point to your program. This is the first thing done. You can add other methods to the same class, but these methods are static methods, since you cannot create an instance of a class that has your main function (at least I don't think you can, I never tried )

The goal of creating a class and definition methods is that you can create objects from this class. For example, in your case you create a class Die. Then you can create Die objects from this class. Think of the class as the model or form from which you create objects. By creating these objects, each of them has its own data members (variables defined in the class). Say you defined two variables in your Die class: die1 and die2, each Die object you create will have a variable die1 and die2, but each Die object may contain different values ​​for these variables.

Now let's assume that you create a method in the die class that modifies these variables (the setter method) and allows it to be called public void setDie1(int value) , then you can change the value of the die1 variable by calling this method on the Die object using something something like myDieObject.setDie1(3) .

If you just put the methods in the same class as your main method, you cannot create multiple objects from the same class.

This may be a complicated concept to get started at first, but it will all clear quickly when you learn more. Hope this helps!

+1
source

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


All Articles