📜 ⬆️ ⬇️

Creating a Simple Java Application

Good day!

This article deals with the organization of the simplest Java application for novice developers who have already studied the concepts of the object approach. But for the experienced, it may also be interesting.

Often, novice developers ask the question: "Where to start creating an application," which classes to use, and so on.
')
Do not take the article as an example of “Hello World” for beginners learning the Java language and object-oriented approach. It is assumed that the reader is already familiar with the Java language, but he has the questions mentioned above.

In this article we answer these questions.

In order to build the application gradually, using the concepts of the object-oriented approach, we will represent all the elements of our application as objects of the corresponding classes. Therefore, the application will also be a separate class. To run the application, we will create an instance of it.

Now in more detail ...

First, the Application class is created — it will be the model for the entire application.

public class Application { } 


Next, create an entry point to the application - the main method:

 public class Application { public static void main(String[] args) { } } 


This method is executed when Java is started with an indication of the base class (Application). In it, we create an instance of the application and initialize it, and then run it. For this we will use the init and run methods:

 public class Application { public void init() { } public void run() { } public static void main(String[] args) { Application application = new Application(); application.init(); application.run(); } } 


In the init method, we do the necessary application initialization. In the run method, the main progress code of the application is located.

The init method can be not used, however, we assume that our application goes through two stages - initialization and launch.

The remaining elements of our application can be divided into model classes and views (windows, panels). For example, imagine that we are creating a simple loan calculator. Then the class CreditCalculator will be a calculator model. In the init method we will initialize the calculator, and in the run we will calculate:

 public class Application { private CreditCalculator calculator; public void init() { calculator = new CreditCalculator(); } public void run() { calculator.setAmount(500000); calculator.setYears(3); calculator.calculate(); System.out.println(" : " + calculator.getMonthlyPayment()); } public static void main(String[] args) { Application application = new Application(); application.init(); application.run(); } } 


Thus, you can start creating an application by creating an Application class and then adding the necessary model classes you need to work with.

Now how to start the application

We will assume that you are using the Eclipse development environment, Intellij IDEA, or NetBeans. And the Application class was created in it. In these environments, to launch, you need to call the context menu and click Run ... or Run As Application in the editor of the Application class.

But still - launching an application from the environment was not our goal, but the goal was to understand from which classes you can start creating the application.

This article is based on the Procode podcast.
RSS: procode.podomatic.com/rss2.xml
iTunes: itunes.apple.com/en/podcast/procode/id529972125

Cool developers, please do not scold - you already know everything! And better scold - then it will be clear what is wrong.

Please leave a constructive comment before assessing the article.

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


All Articles