Put multiple classes in the same file

Asked

Viewed 2,462 times

3

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 in main one of the classes, the others I can’t even call the methods.

Is that right? I can only use main an archive .java class? 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 made

/*
 * 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 a second, which I have done there, in another .java, doesn’t work, like that giovane.calcular() which is defined in a third class

  • 1

    Puts your code and shows where errors occur?

  • Whoa, just a second, I’ll organize

  • 2

    @Giovanemachado puts the code in the question then select it and click the button {} Code sample, that serves to format.

  • @Giovanemachado you can use in main or in the class that contains this method, all classes that are within the package iniciativa13ªera. Is the package name right? Usually you don’t use symbols or letters with accents to name packages. Yours could look like this Iniciativa13 or IniciativaTreze, letter-only.

  • I suffered but I edited. That’s right, I can change but I don’t think it solves the problem

  • the error appears as cannot find a Symbol

  • I checked, the package is the same. I created the classes by clicking on it, there is no different

  • @Giovanemachado tried to remove the symbol ª of the package name?

  • I have removed, also noticed there that appears now "Error while doing Parsing the file", referring to this problem I am trying to solve. Changing the package name was unsuccessful

Show 4 more comments

1 answer

2


Java really 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 gets weird to have a package with the same class name. Classes are objects, packages are like last names of these objects. Is conceptually wrong.

If the calcular() is defined in another class, must use in an object of this class, not in an object that does not have this method.

Follow the comments of not using names with symbols. It works, but has tools, bad, that has problems with this.

I see other problems in this class, including that it generates error at runtime.

See "working" without public classes. And see also with a public class with the same file name (this site always uses the name HelloWorld.java). Also put on the Github for future reference.

Browser other questions tagged

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