Variablen Verständnissfrage Konstanten

tom g.

Aktives Mitglied
Hallo Forum,
ich habe hier 2 Klassen. Die erste Klasse dient als Observer
Java:
package core.launchers;

import java.util.Observer;

import models.ApplicationModelRunLevel;

public class ApplicationLaucher implements Observer{

	public void run() {
		// TODO Auto-generated method stub
		ApplicationModelRunLevel.getInstance().addObserver(this);
	}
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		switch(Integer.parseInt(arg.toString())){
			case ApplicationModelRunLevel.getInstance().PRESTART:
			break;
			case ApplicationModelRunLevel.getInstance().RUN:
			break;
			case ApplicationModelRunLevel.getInstance().RELOAD:
			break;
			case ApplicationModelRunLevel.getInstance().EXIT:
			break;
			case ApplicationModelRunLevel.getInstance().EXIT_ON_ERROR:
			break;
		}
	}

}

Die 2. Klasse ist Observable
Java:
package models;

import java.util.Observable;

public class ApplicationModelRunLevel extends Observable{

	final public int PRESTART = 99;
	public final int RUN = 1;
	public final int RELOAD = 6;
	public final int EXIT = 0;
	public final int EXIT_ON_ERROR = 66;
	public int runLevel = 99;
	private static ApplicationModelRunLevel INSTANCE = new ApplicationModelRunLevel();

	protected ApplicationModelRunLevel(){
		super();
	}
	public int getRunLevel(){
		return this.runLevel;
	}

	public void setRunLevel(int _runLevel){
		this.runLevel = _runLevel;
		this.setChanged();
		this.notifyObservers(_runLevel);
	}

	public static synchronized ApplicationModelRunLevel getInstance() {
		return INSTANCE;
	}
}
In der Observer Klasse erhalte ich jetzt in der switch Methode die Fehlermeldung von Eclipse
case expressions must be constant expressions /src/core/launchers ApplicationLaucher.java line 17

Java:
Line 17 ist case ApplicationModelRunLevel.getInstance().PRESTART:
Die Fehler kommen auch in den anderen cases.
Warum wird nicht erkannt, das z.B. ApplicationModelRunLevel.getInstance().PRESTART als final deklariert wurde?

Danke.
Tom
 
Ja, da könnte Java in der Tat erkennen dass PRESTART immer Konstant ist. In der JLS steht dazu folgendes:

Compile-time constant expressions are used in case labels in switch statements (§14.11) and have a special significance for assignment conversion (§5.2).

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

- Literals of primitive type and literals of type String (§3.10.5)
- Casts to primitive types and casts to type String
- The unary operators +, -, ~, and ! (but not ++ or --)
- The multiplicative operators *, /, and %
- The additive operators + and -
- The shift operators <<, >>, and >>>
- The relational operators <, <=, >, and >= (but not instanceof)
- The equality operators == and !=
- The bitwise and logical operators &, ^, and |
- The conditional-and operator && and the conditional-or operator ||
- The ternary conditional operator ? :
- Parenthesized expressions whose contained expression is a constant expression.
- Simple names that refer to constant variables (§4.12.4).
- Qualified names of the form TypeName . Identifier that refer to constant variables (§4.12.4).

Solche Aufrufe die du da gemacht hast sind also erstmal nicht gültig. Das kannst du aber relativ einfach umgehen.
Mach die Konstanten static und greif so drauf zu:
Code:
ApplicationModelRunLevel.PRESTART

EDIT:
Kleiner Flüchtigkeitsfehler korrigiert, danke 😉
 
Zuletzt bearbeitet von einem Moderator:
Solche Aufrufe die du da gemacht hast sind also erstmal nicht gültig. Das kannst du aber relativ einfach umgehen.
Mach die Konstanten final und greif so drauf zu:
Code:
ApplicationModelRunLevel.PRESTART

Dann muss er die Konstanten nicht nur final, sondern auch static machen, sonst sind sie an Objekte und nicht an die Klasse gebunden.
 
Hallo Ihr 2,

wie könnte ich es denn auf dem richtigen (gültigen) Weg lösen?

Worum es geht ist glaub ich klar.

Danke schon mal.
Tom
 
Ändere deine Konstante in Statische Konstante Variablen um:

Java:
     public static final  int PRESTART = 99;
    public static final int RUN = 1;
   ...


Aufruf:
Java:
  switch(Integer.parseInt(arg.toString())){
            case ApplicationModelRunLevel.PRESTART:
            break;
            case ApplicationModelRunLevel.RUN:
            break;
....


bzw. es macht wahrscheinlich eher Sinn die Konstanten direkt
Java:
 ApplicationLaucher
zu definieren oder du verwendest ein Enum.

Grüße
 
Hallo EikeB,

Solche Aufrufe die du da gemacht hast sind also erstmal nicht gültig. Das kannst du aber relativ einfach umgehen.
Mach die Konstanten static und greif so drauf zu:
Code:
ApplicationModelRunLevel.PRESTART

sorry ich hatte es so verstanden, das man den "ungültigen" Weg umgeht. Nicht das es so gültig ist.

Danke für die Hilfe.

Tom
 

Zurück
Oben