Put multiple classes in the same file

1

In my project I have the class main and created new Java class files with NetBeans to define the objects there.

I can only use one of the classes with main , others can not even call the methods.

Is it anyway? Can I only use% s of% class main ? What if I need more classes?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package iniciativa13ªera;

/**
 *
 * @author Giovane
 */
public class Iniciativa13ªEra {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Jogador giovane = new Jogador();
        giovane.setNomeJogador("Giovane - Ekth");
        giovane.setModDestreza(3);
        giovane.setModTamanho(0);

        giovane.calcular();
    }   

}

This is my main, and below the first class I did

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package iniciativa13ªera;

/**
 *
 * @author Giovane
 */
public class Jogador {
    //Atributos
    String nomeJogador;
    int modTamanho;
    int modDestreza;

    //------------- Métodos Personalizados

    //Muda o nome do Objeto (jogador)
    public void nome (String nome){
        this.setNomeJogador(nome);
    }

    //Muda o tamanho do Objeto (jogador)
    public void tamanho(int valor){
        this.setModTamanho(valor);
    }

    //Muda a destreza do Objeto (jogador)
    public void destreza(int valor){
        this.setModDestreza(valor);
    }

    //--------------- Métodos Especiais
    public String getNomeJogador() {
        return nomeJogador;
    }

    public void setNomeJogador(String nomeJogador) {
        this.nomeJogador = nomeJogador;
    }

    public int getModTamanho() {
        return modTamanho;
    }

    public void setModTamanho(int modTamanho) {
        this.modTamanho = modTamanho;
    }

    public int getModDestreza() {
        return modDestreza;
    }

    public void setModDestreza(int modDestreza) {
        this.modDestreza = modDestreza;
    }

    //---------------- Métodos Construtor
    public Jogador() {
        this.setModTamanho(0);
        this.setModDestreza(0);
    }

And for example, if I try to pull from a second, which I have made there, in another .java , does not work, like that .java that is defined in a third class

    
asked by anonymous 16.10.2017 / 23:29

1 answer

1

Java actually limits having more than one public class per file. All other classes must be internal to the package. Furthermore, the public class must have the same file name.

If you need multiple public classes, put one in each file.

Another point is that it is strange to have a package with the same class name. Classes are objects, packages are like surnames of these objects. It's conceptually wrong.

If calcular() is defined in another class, you should use it on an object of this class, not on an object that does not have this method.

Follow the comments on not using names with symbols. It works, but it has bad tools that have problems with it.

I see other problems in this class, including that it generates run-time error.

See "working" without public classes . And see also a public class with the same file name (this site always uses the name HelloWorld.java ).

    
17.10.2017 / 00:20