Final: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 2: | Zeile 2: | ||
|baustelle=Ja | |baustelle=Ja | ||
|blatt=1 | |blatt=1 | ||
|beschreibung=Wir wollen in unserem Programm dafür sorgen, dass wir entscheiden können, welche Objekte verändert werden dürfen und welche möglichst nicht verändert werden sollten. Dafür gibt es das Schlüsselwort <syntaxhighlight inline lang="Java">final</syntaxhighlight>. Die korrekte Verwendung davon ist wichtig und entsprechend gibt es auch folgende Regeln die beachtet werden sollten: | |||
# Attribute sollten soweit wie möglich final sein | |||
# final ist nur für das jeweilig referenzierte Objekt gültig (Bsp. können Collections wie List final sein, ohne dass deren enthaltene Elemente final sind) | |||
# Der Zustand eines Objektes ist eine unsichtbare Abhängigkeit und kann zu zahlreichen Fehlern führen. Randfälle können demnach auch schnell übersehen werden | |||
|schweregrad=leicht | |schweregrad=leicht | ||
|negativ=<syntaxhighlight lang="Java"> | |||
public class DepartmentBad { | |||
private List<Employee> employees; | |||
public DepartmentBad() { | |||
clear(); | |||
} | |||
public void clear() { | |||
employees = new ArrayList<>(); | |||
} | |||
} | |||
</syntaxhighlight> | |||
|positiv=<syntaxhighlight lang="Java"> | |||
public class DepartmentGood { | |||
private final List<Employee> employees = new ArrayList<>(); | |||
//... | |||
public void clear() { | |||
employees.clear(); | |||
} | |||
} | |||
</syntaxhighlight> | |||
Ein weiteres Beispiel: | |||
<syntaxhighlight lang="Java"> | |||
class Event { | |||
private static final int BASE_FEE = 1000; // constants should be final | |||
private final List<String> participants; // fields should be final if possible | |||
public Event() { | |||
participants = new ArrayList<>(); | |||
} | |||
public void addParticipant(String name) { // final not required for parameters | |||
participants.add(name); // the list is final, its content is not | |||
} | |||
public int calculateProfit(int ticketPrice, int venueCost) { | |||
int fixedCost = BASE_FEE + venueCost; // final is not needed for local variables | |||
return participants.size() * ticketPrice - fixedCost; | |||
} | |||
} | |||
</syntaxhighlight> | |||
|kategorie=Programmierstil | |kategorie=Programmierstil | ||
|weiterlesen=Nein | |weiterlesen=Nein | ||
}} | }} |
Version vom 3. Mai 2024, 15:58 Uhr
🚧 | Diese Seite befindet sich in Bearbeitung | 🚧 |
🤓 | Diese Seite ist eine Bewertungsrichtlinie, die ab Blatt 1 annotiert und ab Blatt 2 abgezogen wird. | 🤓 |
Beschreibung
Wir wollen in unserem Programm dafür sorgen, dass wir entscheiden können, welche Objekte verändert werden dürfen und welche möglichst nicht verändert werden sollten. Dafür gibt es das Schlüsselwort final
. Die korrekte Verwendung davon ist wichtig und entsprechend gibt es auch folgende Regeln die beachtet werden sollten:
- Attribute sollten soweit wie möglich final sein
- final ist nur für das jeweilig referenzierte Objekt gültig (Bsp. können Collections wie List final sein, ohne dass deren enthaltene Elemente final sind)
- Der Zustand eines Objektes ist eine unsichtbare Abhängigkeit und kann zu zahlreichen Fehlern führen. Randfälle können demnach auch schnell übersehen werden
Negativbeispiel
public class DepartmentBad {
private List<Employee> employees;
public DepartmentBad() {
clear();
}
public void clear() {
employees = new ArrayList<>();
}
}
Positivbeispiel
public class DepartmentGood {
private final List<Employee> employees = new ArrayList<>();
//...
public void clear() {
employees.clear();
}
}
Ein weiteres Beispiel:
class Event {
private static final int BASE_FEE = 1000; // constants should be final
private final List<String> participants; // fields should be final if possible
public Event() {
participants = new ArrayList<>();
}
public void addParticipant(String name) { // final not required for parameters
participants.add(name); // the list is final, its content is not
}
public int calculateProfit(int ticketPrice, int venueCost) {
int fixedCost = BASE_FEE + venueCost; // final is not needed for local variables
return participants.size() * ticketPrice - fixedCost;
}
}