/ I want to create a date object, where the day and the year are of type int, and the month I intend to be an enumerated type, but in the instantiation when passing the month argument it gives me an error, what am I doing wrong? /
public class PROG03_Ejerc1 {
public static void main(String[] args) {
Fecha objFecha = new Fecha(4,Abr,1976); // Abr cannot be resolved to a variable
}
}
class Fecha {
private enum enumMes {Ene,Feb,Mar,Abr,May,Jun,Jul,Ago,Sep,Oct,Nov,Dic};
private int diaDelMes;
enumMes mes;
private int año;
public Fecha(int diaDelMes, enumMes mes, int año) {
this.diaDelMes = diaDelMes;
this.mes = mes;
this.año = año;
}
}
Your enum declaration is private to the Date class, so in main you can't use "Apr" because I guess it should give you undefined variable. Cast the enum out or declare it as public static on Date and use Date.Apr.
It is recommended to declare the enum as a variable in the same class and take its value from there:
But here's one way to do it by taking the values of del
enum
from the classFecha
, declaring tuenum
as follows:so you will have no problem declaring the month: