Question:
In my project I have the main
class and I created new Java class files with NetBeans to define the objects there.
I can only use one of the classes in main
, the others I can't even call the methods.
Is that so? Can I only use a .java
class file in 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 is 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 a second one, which I have made there, in another .java
, it doesn't work, like that giovane.calcular()
that is defined in a third class
Answer:
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 name as the file.
If you need multiple public classes, put one in each file.
Another point is that it is weird to have a package with the same name as the class. Classes are objects, packages are like the surnames of these objects. It is conceptually wrong.
If calcular()
is defined in another class, it must be used on an object of that class, not on an object that does not have this method.
Follow the comments of not using names with symbols. It works, but there are tools, bad ones, that have problems with it.
I see other problems in this class, including that it generates an error at runtime.
See "working" without public classes . And see also with a public class with the same name as the file (this site always uses the name HelloWorld.java
). Also posted on GitHub for future reference .