Error: non-static variable this cannot be referenced from a Static context

Asked

Viewed 736 times

0

This code is giving error:

non-static variable this cannot be referenced from a Static context

When I take the static main does not look like the warnings in Netbeans, but when compiling it makes an error because it is missing static. What do I do? How do I turn it?

package Poo2at2;

import java.util.ArrayList;


public class Main {
    
    public ArrayList<Filme> filme = new ArrayList<Filme>(11);
    public ArrayList<Aula> aula = new ArrayList<Aula>(11);
    public ArrayList<Classe> classe = new ArrayList<Classe>(11);
    public Somador somador = new Somador();
        
    
    public static void main(String[] args) {
        
        System.out.println(somador.somar(this.filme));
        System.out.println(somador.somar(this.aula));
        System.out.println(somador.somar(this.classe));
        
    }

}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

2 answers

1

Do what the error message shows is wrong. If it says there is no static variable it is because it needs the variable to be static, right? So make the variables static, after all they are accessed in a static method so can only access static variables, the variables the way they are there could only be accessed by instantiating the class Main and access through this object.

import java.util.ArrayList;

public class Main {
    public static ArrayList<Filme> filme = new ArrayList<Filme>(11);
    public static ArrayList<Aula> aula = new ArrayList<Aula>(11);
    public static ArrayList<Classe> classe = new ArrayList<Classe>(11);
    public static Somador somador = new Somador();
    
    public static void main(String[] args) {
        System.out.println(somador.somar(filme));
        System.out.println(somador.somar(aula));
        System.out.println(somador.somar(classe));
    }
}

I put in the Github for future reference.

Actually this concept of having the method main() in the same class as having lists that do something might even work, but it’s conceptually wrong. If training the error is what you will learn, then I suggest using a more structured learning methodology that shows how to do, try and error trains the error more. I can not talk about other problems because I have not seen the whole code, but there are indications that there are other errors just reading this excerpt, may be trained several concepts in the wrong way.

0

Understand the modifier static. A definition would be that when we declare a variable as Static, it becomes an attribute of the class and not of the instance. We can also think of this variable as a global attribute of a class.

Take an example:

Class with 2 attributes one static and one non-static.

 public class ClasseComAtributoStatic {
        
        public static int contadorStatic;
        public int contadorInstacia;
        
        public ClasseComAtributoStatic() {
            contadorStatic++;
            contadorInstacia++;
        }
        
    }

Test class.

public class TesteStatic {

public static void main(String[] args) {
    ClasseComAtributoStatic classeComAtributoStatic = new ClasseComAtributoStatic();
    System.out.println("1- Atributo static (contadorStatic): " + classeComAtributoStatic.contadorStatic);
    System.out.println("2- Atributo instancia  (contadorInstacia): " + classeComAtributoStatic.contadorInstacia);
    
    ClasseComAtributoStatic classeComAtributoStatic2 = new ClasseComAtributoStatic();
    System.out.println("3- Atributo static (contadorStatic): " + classeComAtributoStatic2.contadorStatic);
    System.out.println("4- Atributo instancia (contadorInstacia): " + classeComAtributoStatic2.contadorInstacia);
    
    ClasseComAtributoStatic classeComAtributoStatic3 = new ClasseComAtributoStatic();
    System.out.println("3- Atributo static (contadorStatic): " + classeComAtributoStatic3.contadorStatic);
    System.out.println("4- Atributo instancia (contadorInstacia): " + classeComAtributoStatic3.contadorInstacia);
    
    
    System.out.println("Reimprimindo atributo static (contadorStatic) da primeira isntacia: " + classeComAtributoStatic.contadorStatic);

}

}

Result of the execution of the Testestatic class

1- Atributo static (contadorStatic): 1
2- Atributo instancia  (contadorInstacia): 1
3- Atributo static (contadorStatic): 2
4- Atributo instancia (contadorInstacia): 1
3- Atributo static (contadorStatic): 3
4- Atributo instancia (contadorInstacia): 1
Reimprimindo atributo static (contadorStatic) da primeira isntacia: 3

Let’s understand what happened here. I want to know how many times my class ClasseComAtributoStatic was instantiated by the builder I created. Knowing which static attribute belongs to the class and not to each instance, every time the constructor executes the static attribute contadorStatic is incremented and "keeps" its value until the end of execution. The attribute contadorInstacia is also incremented but it belongs to each instance of the object, so its value is always in 1. That is, comparing the two attributes in the result of the TesteStatic we will see that the contadorStatic increments and "stores" the amount of times the constructor was invoked.

I strongly recommend that you create and test these two classes to better understand how a Static attribute works. I also recommend reading and exercises of this link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.