also was will mir die Aufgabe hier mitteilen? wann wird das instance Feld denn jetzt initialisiert? und wie tausche ich das von "vorher" auf "nachher"?
readKeyFromConsoleAndInitPlanet liest doch von der Konsole? oder warum geht das verloren?
und die klassen sind in separaten Dateien?? die Solution klasse ist nach dem Catch ja beendet
package de.codegym.task.task15.task1522;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Singleton-Muster festigen
*/
public class Solution {
public static void main(String[] args) {
}
public static Planet thePlanet;
static{
readKeyFromConsoleAndInitPlanet();
}
public static void readKeyFromConsoleAndInitPlanet() {
BufferedReader reader = null;
String key = null;
try{
reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
key = reader.readLine();
if(key == null){
throw new IOException("irgendwas");
}
if(key.equals("sun")){
thePlanet = Sun.getInstance();
}
else if(key.equals("moon")){
thePlanet = Moon.getInstance();
}
else if(key.equals("earth")){
thePlanet = Earth.getInstance();
}
else{
thePlanet = null;
}
}
}
catch(IOException e){
}
}
}
class Sun implements Planet {
private static Sun instance;
private Sun(){
}
public static Sun getInstance(){
return instance;
}
}
class Moon implements Planet {
private static Moon instance;
private Moon(){
}
public static Moon getInstance(){
return instance;
}
}
class Earth implements Planet {
private static Earth instance;
private Earth(){
}
public static Earth getInstance(){
return instance;
}
}