Stef: OOP-Grundlagen

Beitrag lesen

Moin,

folgende Datei oop1.java erzeugt beim Kompilieren einen Fehler:

public class oop1
{

public class Auto
 {
  public String name;
  public int wert;
  public int leistung;
 }

public static void main(String[] args)
 {
 // Beispielauto
 Auto eins = new Auto();

eins.name = "Auto A";
 eins.wert = 25000;
 eins.leistung = 170;

System.out.println("Name........: " + eins.name);
 System.out.println("Wert........: " + eins.wert);
 System.out.println("Leistung....: " + eins.leistung);
 }

}

Folgender Fehler wird von javac angezeigt:

oop1.java:20: non-static variable this cannot be referenced from a static contex
t
        Auto eins = new Auto();
                    ^
1 error

Beispiel ist abgeleitet aus "Handbuch der Java-Programmierung", siehe http://javabuch.de.