Cannot make a static reference to the non-static field

annequin

Mitglied
Hallo!

Ich bekomme diese Fehlermeldung: "Cannot make a static reference to the non-static field temperature"
Bei diesem Quellcode, der zur Berechnung der Druchschnittstemperatur dient:

Java:
public class wetterstation {
	
	int [] temperature = {12, 14, 9, 12, 15, 16, 15, 15, 11, 8, 13, 13, 15, 12};

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		int summe=0;
		
		for(int i=0; i < temperature.length; i++)
		{
			summe = summe + temperature[i];
		}
	
	int average = summe / temperature.length;

Sobald ich die Variable temperature in der Methode erstelle, gibt es keine Probleme, ich brauche die jedoch auch noch für weitere Methoden, eine lokale Variable nützt mir also nichts...
Wo liegt mein Fehler? Wieso kriege ich diese Fehlermeldung?

Beste Grüße
annequin
 

njans

Top Contributor
public static void main(String[] args)

Der Eintiegspunkt ist static. Und dein int[] temperature ist teil jedes Objektes wetterstation.
Entweder machst du temperature static oder du erstellt ein Objekt von wetterstation und verwendest das interne int[]
 

annequin

Mitglied
Hilfe......

Java:
public class wetterstation {
	
	int [] temperature = {12, 14, 9, 12, 15, 16, 15, 15, 11, 8, 13, 13, 15, 12};

	
	public void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		int summe=0;
		
		for(int i=0; i < temperature.length; i++)
		{
			summe = summe + temperature[i];
		}
	
	int average = summe / temperature.length;
	
	System.out.println("Die Durchschnittstemperatur beträgt " + average + "°C");
	}
	
	public void getMin()
	{
		int min = 0;
		
		for(int i = 0; i < temperature.length)
	}
	

}

Jetzt hakt es in Zeile 26, selber Fehler..... ich bin verwirrt :noe:
 

njans

Top Contributor
Da fehlen schließende und öffnende Klammer der for-Schleife.

Und
Java:
 for(int i=0; i < temperature.length; i++)
        {
            summe = summe + temperature[i];
        }

Wird so nicht compilieren, da temperature immer noch teil von einem Objekt ist.


Hier hast du mehrere Möglichkeiten:

Java:
    public class wetterstation {
       
        static int [] temperature = {12, 14, 9, 12, 15, 16, 15, 15, 11, 8, 13, 13, 15, 12};
     
        public void main(String[] args) {
            int summe=0;
            for(int i=0; i < temperature.length; i++)
            {
                summe = summe + temperature[i];
            }
            int average = summe / temperature.length;
            System.out.println("Die Durchschnittstemperatur beträgt " + average + "°C");
        }
    }

Java:
    public class wetterstation {
       
        int [] temperature = {12, 14, 9, 12, 15, 16, 15, 15, 11, 8, 13, 13, 15, 12};
     
        public void main(String[] args) {
            wetterstation w = new wetterstation();
            int summe=0;
            for(int i=0; i < w.temperature.length; i++)
            {
                summe = summe + w.temperature[i];
            }
            int average = summe / w.temperature.length;
            System.out.println("Die Durchschnittstemperatur beträgt " + average + "°C");
        }
    }

Oder, wenn man es schöner machen will:
Java:
public class Wetterstation
{
	private int[] temperature	= { 12, 14, 9, 12, 15, 16, 15, 15, 11, 8, 13, 13, 15, 12};

	public void main(String[] args)
	{
		Wetterstation w = new Wetterstation();
		w.printAverageTemperatur();
	}
	
	public void printAverageTemperatur()
	{
		int summe = 0;
		for (int i = 0; i < temperature.length; i++)
		{
			summe = summe + temperature[i];
		}
		
		int average = summe / temperature.length;
		System.out.println("Die Durchschnittstemperatur beträgt " + average + " °C");
	}
}
 
Zuletzt bearbeitet:

Maskin

Mitglied
Schreib doch mal die Methode zu ende.... oder hast du hier nicht den ganzen Code geposted?
Wenn du die Schleife mal fertig stellen würdest sollte es auch wieder gehen.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
M Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 10
L Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 6
K Variablen Cannot make a static reference to the non-static field time Java Basics - Anfänger-Themen 6
P Compiler-Fehler Cannot make a static reference to the non-static field process Java Basics - Anfänger-Themen 3
F Mal wieder: Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 9
F Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 3
B Cannot make a static reference...?? Java Basics - Anfänger-Themen 5
B Mal wieder "cannot make a static reference..." Java Basics - Anfänger-Themen 2
R Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 5
L [Gelöst] Cannot make a static reference ... Java Basics - Anfänger-Themen 12
T Cannot make a static reference to the non-static field Java Basics - Anfänger-Themen 8
frau-u Altes Problem: non-static method cannot be reference Java Basics - Anfänger-Themen 7
J Fehlermeldung unklar. non-static variable player0 cannot be referenced from a static context Java Basics - Anfänger-Themen 4
P non-static variable cannot be referenced from a static context Java Basics - Anfänger-Themen 6
Aprendiendo Interpreter-Fehler "non-static variable this cannot be referenced from a static context" Java Basics - Anfänger-Themen 2
H Variablen error: non-static variable cannot be referenced from a static context Java Basics - Anfänger-Themen 4
U Erste Schritte cannot be referenced from a static context Java Basics - Anfänger-Themen 1
W Compiler-Fehler "non-static method cannot be referenced"-Problem Java Basics - Anfänger-Themen 6
M Methoden "Non-static method xy cannot be referenced from a static context" Java Basics - Anfänger-Themen 20
K Objekt erstellen - error: non-static variable this cannot be referenced from a static context Java Basics - Anfänger-Themen 17
H non-static method cannot be referenced from a static context Java Basics - Anfänger-Themen 2
N cannot referenced from static context Java Basics - Anfänger-Themen 5
Q non-static method blub cannot be referenced from a static context Java Basics - Anfänger-Themen 6
N non-static variable con cannot be referenced from a static context Java Basics - Anfänger-Themen 7
G non static method scale(int,int) cannot be referenced from a static context Java Basics - Anfänger-Themen 16
J non static method cannot be referenced from static context Java Basics - Anfänger-Themen 7
P Problem mit Thread - cannot be referenced from a static cont Java Basics - Anfänger-Themen 5
W Cannot find Symbol Java Basics - Anfänger-Themen 5
M NullPointerException: Cannot read the array length because "this.Kinder" is null Java Basics - Anfänger-Themen 1
D Cannot find JUnit.framework Java Basics - Anfänger-Themen 1
W Cannot resolve symbol 'HttpServlet' Java Basics - Anfänger-Themen 2
I JSON - cannot deserialize from Object value Java Basics - Anfänger-Themen 16
J Scanner cannot be resolved to a type Java Basics - Anfänger-Themen 3
N Fehler "Cannot instantiate the type" Java Basics - Anfänger-Themen 3
jakobfritzz Array- cannot invoke "" because "" is null Java Basics - Anfänger-Themen 4
Flo :3 Variablen Type dismatch: cannot convert from string to int Java Basics - Anfänger-Themen 9
C system cannot be resolved Fehler in Eclipse Java Basics - Anfänger-Themen 18
V ClientProtocolException cannot be resolved Java Basics - Anfänger-Themen 6
A Cannot find symbol mit Konstruktoren Java Basics - Anfänger-Themen 27
A Cannot find symbol bei exceptions Java Basics - Anfänger-Themen 2
J The import org.bukkit cannot be resolved Java Basics - Anfänger-Themen 3
L cannot find symbol variable Kon Java Basics - Anfänger-Themen 8
L constructor cannot be applied... Java Basics - Anfänger-Themen 22
F Erste Schritte error: cannot find symbol Java Basics - Anfänger-Themen 5
P a cannot be resolved bei einer do while Schleife Java Basics - Anfänger-Themen 1
R return: cannot find symbol Java Basics - Anfänger-Themen 2
L Bluej Error: Cannot find Symbol Java Basics - Anfänger-Themen 13
M Iterator cannot refer to a non final... Java Basics - Anfänger-Themen 20
S Cannot find symbol (symbol ist eine Variable) Java Basics - Anfänger-Themen 13
N Cannot find symbol Java Basics - Anfänger-Themen 18
T Error: int cannot be dereferenced Java Basics - Anfänger-Themen 10
J JLabel cannot be resolved Java Basics - Anfänger-Themen 8
P Cannot find symbol, wieso? Java Basics - Anfänger-Themen 5
UnityFriday method getPrevious in class List<ContentType> cannot be applied to given types Java Basics - Anfänger-Themen 29
M Erste Schritte cannot find symbol - Probleme mit Klassen Java Basics - Anfänger-Themen 6
B OOP next cannot be resolved or is not a field Java Basics - Anfänger-Themen 6
B OOP Cannot instantiate the type AuDList<Integer> Java Basics - Anfänger-Themen 18
J Error: cannot find symbol - variable Java Basics - Anfänger-Themen 3
D Java Eclipse cannot be cast to java.awt.event.ItemListener Java Basics - Anfänger-Themen 3
F Erste Schritte parseint: cannot find symbol Java Basics - Anfänger-Themen 6
J Fehlermeldung : cannot invoke char(at) int on the primitive type int --- Anfänger Java Basics - Anfänger-Themen 5
M Erste Schritte [Variable] cannot be resolved to a variable Java Basics - Anfänger-Themen 4
M The Selection cannot be launched... Java Basics - Anfänger-Themen 4
M Vererbung - Cannot Find Symbol constructor... Java Basics - Anfänger-Themen 11
D error: cannot find symbol Java Basics - Anfänger-Themen 3
B Frage zu Beispielprogramm: "error: cannot find symbol" Java Basics - Anfänger-Themen 2
M Methoden Cannot be resolved to a variable Java Basics - Anfänger-Themen 5
BlueMountain Erste Schritte error: cannot find symbol Java Basics - Anfänger-Themen 2
P enum: cannot be resolved to a type Java Basics - Anfänger-Themen 2
L Error: Cannot find symbol Java Basics - Anfänger-Themen 1
W int cannot be dereferenced Java Basics - Anfänger-Themen 5
P Cannot find Symbol Java Basics - Anfänger-Themen 3
K Compiler-Fehler Button cannot be resolved or is not a field Java Basics - Anfänger-Themen 6
F Methoden Cannot refer to a non-final variable.. verständnisproblem. Java Basics - Anfänger-Themen 7
K The Java Runtime Environment cannot be found. Java Basics - Anfänger-Themen 6
F Input/Output IOTools Fehlermeldung: cannot be resolved Java Basics - Anfänger-Themen 16
L Cannot Find Symbol - Was soll denn das bedeuten?!? Java Basics - Anfänger-Themen 7
P StdIn.readDouble: cannot find symbol Java Basics - Anfänger-Themen 7
B Fehler "Cannot find symbol - variable number1" Java Basics - Anfänger-Themen 13
P Variablen Cannot be resolved to a variable Java Basics - Anfänger-Themen 8
B Compiler-Fehler cannot find symbol Java Basics - Anfänger-Themen 6
K Date cannot be cast to java.lang.Integer Java Basics - Anfänger-Themen 4
T Cannot convert from Integer to int Java Basics - Anfänger-Themen 12
K Cannot find symbol Java Basics - Anfänger-Themen 3
H cannot find symbol Java Basics - Anfänger-Themen 4
S cannot find symbol, symbol: constructor Java Basics - Anfänger-Themen 2
3 Compiler-Fehler Fehlerbehebung cannot find Symbol Java Basics - Anfänger-Themen 4
R Compiler-Fehler Cannot find symbol (Method printIn) Java Basics - Anfänger-Themen 3
M Meldung "cannot convert flom boolean to boolean[]" Java Basics - Anfänger-Themen 3
B Polymorphie A obj = new B; "cannot find symbol app()" Java Basics - Anfänger-Themen 5
S wieso Fehlermeldung cannot find symbol hier Java Basics - Anfänger-Themen 10
T Cannot refer to an instance field xxx while explicitly invoking a constructor Java Basics - Anfänger-Themen 14
T Cannot find Symbol(String) Java Basics - Anfänger-Themen 9
C cannot convert from int to boolean Java Basics - Anfänger-Themen 3
2 Compiler-Fehler cannot find symbol Java Basics - Anfänger-Themen 13
N Erste Schritte import javax.media.3d....; //Fehlermeldung: cannot be resolved Java Basics - Anfänger-Themen 10
B Erste Schritte cannot find symbol - problem Java Basics - Anfänger-Themen 9
K int cannot be dereferenced Java Basics - Anfänger-Themen 2
H LocationReferenceImpl cannot be resolved to a type Java Basics - Anfänger-Themen 5
D Cannot find symbol variable Java Basics - Anfänger-Themen 9

Ähnliche Java Themen

Neue Themen


Oben