Do not compile non-static variable and package does not exist

Asked

Viewed 27 times

0

Can help me please, I’m studying Java and I’m having the following mistakes:

Main.java:12: error: non-static variable carro1 cannot be referenced from a static context
    carro1 = new aut.automovel("Chevette", 87);
    ^ Main.java:12: error: package aut does not exist
    carro1 = new aut.automovel("Chevette", 87);

This program has three classes: Automoveis, Main and Motorista.

import java.io.*;

public class Main
{
    private Automoveis carro1;
    private Motorista motorista1, motorista2;

    public static void main(String[] args)
    {
        Automoveis aut = new Automoveis();
        Motorista mot = new Motorista();
        carro1 = new aut.automovel("Chevette", 87);
        //carro1 = new aut.Automoveis("T",78);
        aut.displayMessage("Teste");
        //motorista1 = new mot.motorista("João", carro1);

        //motorista2 = new mot.motorista("Pedro",carro1);

        //System.out.println(motorista1.obterNome());
        //System.out.println(motorista2.obterNome());
    }
}

-

public class Automoveis
{
    public String modelo;
    public int ano;
    private boolean ligado;

    //Instanciar automaovel
    public void automovel(String m, int a){
        modelo = m;
        ano = a;
        ligado = false;
    }

    //Ligar automóvel
    public void liga()
    {
        ligado = true;
    }

    public void desliga()
    {
        ligado = false;
    }

    public void displayMessage(String s)
    {
        System.out.println(s);
    }
}

-

public class Motorista
{
    private String nome;
    private Automoveis carro;

    //Instanciar motorista
    public void motorista(String n, Automoveis a)
    {
        nome = n;
        carro = a;
    }

    //Obter nome do motorista
    public String obterNome()
    {
        return nome;
    }
    public Automoveis obterCarro()
    {
        return carro;
    }

}

What I’m doing wrong?

Thanks in advance!

1 answer

2


public void automovel(String m, int a){

Now, that was supposed to be a builder, but because of the presence of that void, is actually a method. What you wanted was this:

public Automoveis(String m, int a) {

In the method main, instead:

    Automoveis aut = new Automoveis();
    Motorista mot = new Motorista();
    carro1 = new aut.automovel("Chevette", 87);

You should have done it:

    Automoveis carro1 = new Automoveis("Chevette", 87);
    Motorista mot = new Motorista();

Note that when using new aut.automovel you are trying to invoke a constructor (that’s what the use of new means). But the method automovel is not a builder.

Ah, and it is worth mentioning that it is good practice to put the name of the classes in the singular. Therefore, prefer to use Automovel instead of Automoveis.

  • Solved, that was it. As for plural did not know, I will use from now on! Thanks.

  • @Brunosouza If this answer solved your problem and there is no doubt left, click on the " " which is to the left of the answer to mark it as accepted/correct, which also marks your question as solved/answered. Otherwise, feel free to comment.

Browser other questions tagged

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