Nur Main
| 🚧 | Diese Seite befindet sich in Bearbeitung | 🚧 |
| 🤓 | Diese Seite ist eine Bewertungsrichtlinie, die ab Blatt 1 annotiert und ab Blatt 2 abgezogen wird. | 🤓 |
Beschreibung
Die Main-Methode public static void main (String[] args) ist lediglich der Eingangspunkt für das Programm. Sie sollte deshalb so kurz wie möglich sein und keine weiteren Aspekte behandeln.
Besonders heißt das, dass nie das gesamte Programm in der Main-Methode sein sollte. Nutzen Sie mehrere Klassen und Methoden, um das Programm nach den Prinzipien der Objektorientierung und des Geheimnisverrats zu strukturieren.
In Kürze
- So kurz wie möglich halten
- Nicht das gesamte Programm in der Main-Methode implementieren.
- Die Funktionalität auf mehrere Klassen und Methoden aufteilen
Was soll ich in der Main-Methode machen?
- Validieren der Parameter, die für das Starten des Programms wichtig sind und demnach nicht an das Programm selbst übergeben werden können.
- Laden einer Konfigurationsdatei.
- Erzeugen und Starten der Hauptanwendung (Applikation-Klasse).
- Abfangen von Exceptions, die an anderen Stellen nicht behandelt werden können, und Ausgeben einer entsprechenden Fehlermeldung.
Positivbeispiel
public static void main(String[] args) {
// Validieren der Parameter
if (args.length < 1) {
System.err.println("Please enter the path to configuration file");
return;
}
Config config;
try {
// Laden der Konfigurationsdatei
config = ConfigLoader.load(args[0]);
} catch (IllegalArgumentException exception) {
// Abfangen von Exceptions
System.err.println("The configuration file could not be loaded:" +
exception.getMessage());
return;
}
// Erzeugen und Starten der Hauptanwendung
Application app = new Application(config);
app.run();
}
Beispiele
Negativbeispiel:
public class Main {
public static void main(String[] args) {
if (args.length < 2) {
return;
}
int x;
int y;
try {
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);
} catch (NumberFormatException exception) {
System.err.println(exception.getMessage());
return;
}
int result;
if (x < 0 || y < 0) {
System.err.println("Negative numbers are not allowed.");
return;
} else {
result = x + y;
}
System.out.println("Result: " + result);
}
}
Positivbeispiel:
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
Parser parser = new Parser();
int x;
int y;
try {
x = parser.parseX(args);
y = parser.parseY(args);
} catch (IllegalArgumentException exception) {
System.err.println("Parsing failed:" + exception.getMessage());
return;
}
calculator.add(first, second);
}
}
public class Calculator {
public void add(int x, int y) {
if (x < 0 || y < 0) {
System.err.println("Negative numbers are not allowed.");
return;
}
int result = x + y;
System.out.println("Result: " + result);
}
}
public class Parser {
public int parseX(String[] args) {
if (args.length < 1) {
// Throws exception
}
return Integer.parseInt(args[0]);
}
public int parseY(String[] args) {
// Parses the passed in y and checks if correct
}
}