Skip to content
Advertisement

Problem at calling a constructor if the java class is in a different path

I want to save the java classes in a folder called Classes, so on NetBeans, I have created that folder, and then I have saved it the class called Jugadores.java but after doing that i am having issues to call my constructor called regisPlayer on the main class. NetBeans says that:

cannot find the simbol
symbol:class regisPlayer

location: class Obligatorio

This is my main class

package obligatorio;
import java.util.*;
import obligatorio.classes.*;
public class Obligatorio {

public static void main(String[] args) {
    Jugadores();
}

static void Jugadores() {
  Scanner in = new Scanner(System.in);
  System.out.println("Player Name ");
  String Name = in.nextLine();

  System.out.println("Age Player ");
  int Edad = in.nextInt();

  Jugadores player = new regisPlayer(Name, Edad); // On this line says than can not find the symbol regisPlayer

}

}

This is my class Jugadores.java

package obligatorio.classes;

public class Jugadores {
    private String nombre;
    private int edad;
 
    public void regisPlayer(String Nombre, int Edad) {
        this.nombre(Nombre);
        this.edad(Edad);
    }

    public void nombre(String Nombre) {
        nombre = Nombre;
    }
    public void edad(int Edad) {
        edad = Edad;
    }

}

I do not what it could be the problem, I am learning Java. I have tried to solve the problem adding the name package obligatorio.classes in Jugadores.java to then call it on my main class but I did not work.

Advertisement

Answer

package obligatorio.classes;

public class Jugadores {
    private String nombre;
    private int edad;

    public Jugadores(String nombre, int edad) { //this is a constructor
       this.nombre = nombre;
       this.edad = edad;
    }

    public void setNombre(String Nombre) { //only need setters if you plan to change it 
        nombre = Nombre;
    }
    public void setEdata(int Edad) {//only need setters if you plan to change it 
        edad = Edad;
    }
}

Used like this :

Jugadores player = new Jugadores("name example", "edad example");
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement