DEV Community

Cover image for How Java's classes and main method work?
JC Smiley
JC Smiley

Posted on

How Java's classes and main method work?

The purpose of this blog post is to explain the flow of a simple Java app and how to use classes to organize the data.

Imagine that we are developing a restaurant app that allows the user to display a menu. This menu has menu items like cheese cake (dessert), burgers (main course), and fried pickles (appetizer). You want the user to be able to:

  • View the entire menu
  • View specific menus like dessert, main course, appetizers
  • Add/Remove menu items

What is the purpose of the main method
public class Main {
   public static void main(String[] args) {}
Enter fullscreen mode Exit fullscreen mode

The file with the main method is the entry point into the application. It's goal is to present a way for the user to manipulate the application or to run a series of instructions. This is where the user can input data or where the data for the application is set up. For this restaurant application, the user should be able to view and manipulate menus.

Why do we need more Classes

The purpose of additional classes is to provide the structure of the data that is to be updated by the application. For this restaurant application, the user will view a menu that has menu items. Naturally, we need classes that represent a menu and menu item.

  • ==> Entry point into the app
  • ==> Menu object that displays menu items in a menu and add/remove the menu items.
  • ==> MenuItem object that displays meal properties like price.

Those objects (classes) have properties (data) and functions (methods to manipulate the data). An example is the price property of the MenuItem class and an updatePrice() function that changes that price.

Let's talk about Flow

Java's Main Class

  1. The user runs the application from the main method in the Main class.
  2. From the image above, a menu (from the Menu class) is created and displayed for the user to view on lines 1-2. If you notice, there is no displayMenu() function in the Main class. That method is a property of the Menu object created from the Menu class template.
  3. Two menuItem (from the MenuItem class) objects are added to the current menu on lines 3-4. Pay attention to the fact that the file does not have a addItemToMenu() function. That function is a property of the Menu class. I want to reiterate that the main function job is just to present data and allow the user to manipulate the application. It uses the other classes to create and manipulate data.
  4. The updated menu object is displayed again for the user on line 5.
  5. The menu object uses one of its functions, removeItemToMenu(), to update itself on line 6.
  6. The final line displays an updated menu object.


Remember, the class with the main method is the entry point of the app. Its job is to either run a series of instructions, present data to the user, or allow the user to manipulate the app. The data that is manipulated is formed from a class's object.
visual explination

Latest comments (0)