Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class method vs main methods

Tags:

java

methods

I'm rather confused by the concept of making a class with methods, and having a main method with methods underneath.

What exactly are the differences here? How it one way better than the other way? What situations call for a method under the main rather than a class?

It would help me to understand a project - a basic Craps game which will contain a Shooter class, Die class, and a Craps class (which apparently contains the main).

like image 650
Miranda Avatar asked Jul 14 '26 18:07

Miranda


2 Answers

You do not have "main" methods. All methods are a class method, the only difference is that 'static' methods (v.g., main) do not need that you instantiate a new object (via the 'new' statement) to use them.

In other words:

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

  public void myInstanceMethod() {
  }
}

You can do

MyClass.myStaticMethod()

but in order to use myInstanceMethod you must create an object

(new MyClass).myInstanceMethod;

Usually you do the last thing as

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

Note that you can also do

myObject.myStaticMethod();

but it is exactly the same than doing

myClass.myStaticMethod();

and the first way is considered poor style and usually causes a compiler warning.


@Miranda because with only static methods you lose all of the Object Oriented part, and you just end using Java as you would use plain C.

In the object, you have both the state and the methods. In the class you store both the object state and the methods. For instance, typically you can create a "Card" class, create a card "new Card('K', "Leaf")" and have methods to manipulate it ("uncoverCard()").

Once you have the reference to the object that you want, you use its methods and you know that you are only affecting this object, and that you are using the right version of the method (because you are using the method defined for this very class).

Switching to OO programming from procedural programming may seem difficult at the beginning. Keep trying, looking at tutorial code and asking for advice when needed and you'll soon get to understand it.

like image 193
SJuan76 Avatar answered Jul 17 '26 17:07

SJuan76


Basically it's a matter of separation. You create classes for reusability. If everything was 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 wouldn't be able to take advantage of all that object oriented programming affords you. You'll learn more about it as you get into the course. But basically each of the classes you will create will have single responsibility. Even the main class, whose responsibility is to run the program.

Good luck with your class by the way. Maybe it will give you an advantage in the casinos.

like image 43
ek_ny Avatar answered Jul 17 '26 17:07

ek_ny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!