Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
How to call getSeason(Months.OCTOBER)?

My question is in relation to enums and switch expressions.

I am experiencing difficulties when attempting to call method by using getSeason(Months.OCTOBER).

How can I call this method in main method to print “SPRING”? I don’t think the problem is in Season method, but rather in main() method.

Code:

public class Seasons {
  private static Seasons season;

  enum Seasons {SPRING, SUMMER, WINTER, AUTUMN};

  enum Months { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, 
    SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER};

  public static Seasons getSeason(Months month) {
    Seasons season = null;
    switch (month) {
      case SEPTEMBER:
      case OCTOBER:
      case NOVEMBER:
        season = Seasons.SPRING;
        break;
      case DECEMBER:
      case JANUARY:
      case FEBRUARY:
        season = Seasons.WINTER;
        break;
      case JUNE:
      case JULY:
      case AUGUST:
        season = Seasons.SUMMER;
        break;
      case MARCH:
      case APRIL:
      case MAY:
        season = Seasons.AUTUMN;
        break;
    }
    return season;
  }

  public static void main(String[] args) {

    getSeason(Months.OCTOBER); //How can I print “SPRING” ?

  }
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The Answer by Alexander Ivanchenko is correct. I'll add a bit about naming, and some tips.

We already have the java.time.Month enum. No need to re-invent.

Generally, an enum should be named in the singular. For example, java.time.Month and java.time.DayOfWeek. So, Season rather than Seasons.

The java.time classes (JSR 310) established some naming conventions that have proven to be sensible and handy. For moving from the more specific to the more general, meaning we are losing information, use from. For example, java.time.YearMonth offers a static factory method from to produce a year-month value from a date object, etc. So Season#from( java.time.Month ).

And consider adding a method to produce a display name, versus the all-caps name of the enum object. For a small specific project, hard-code the result of such a method, for a specific language. If re-using this class, consider adding localization via a passed Locale argument as seen on the java.time classes. For example, java.time.Month#getDisplayName( TextStyle , Locale ).

enum Season {
        SPRING ( "Spring" ), 
        SUMMER ( "Summer" ), 
        WINTER ( "Winter" ), 
        AUTUMN ( "Autumn" );
    
    private String displayName ;
    
    Season ( String displayName ) {
        this.displayName = displayName ;
    }
    
    public String getDisplayName() { return this.displayName ; }
    
    public Season from( java.time.Month month ) {
        Objects.requireNonNull( month ) ;
        return switch ( month ) {
            case SEPTEMBER, OCTOBER, NOVEMBER -> Season.SPRING;
            case DECEMBER, JANUARY, FEBRUARY -> Season.WINTER;
            case JUNE, JULY, AUGUST -> Season.SUMMER;
            case MARCH, APRIL, MAY -> Season.AUTUMN;
        };
    }
}

Usage:

System.out.println( Season.from( Month.JANUARY ) ) ;
System.out.println( Season.from( Month.JANUARY ).getDisplayName() ) ;

WINTER

Winter

By the way, a tip: An enum can now be defined locally, in Java 16 and later. This came from work on records.

over 4 years ago · Santiago Trujillo Report

0

Method getSeason() does its job correctly, the reason you don't see anything on the console is that the result of the method call is ignored.

Simply do this:

System.out.println(getSeason(Months.OCTOBER));

By the way, if you are using Java 14 + your getSeason() method could be implemented in more concise fashion.

    public static Seasons getSeason(Months month) {
        return switch (month) {
            case SEPTEMBER, OCTOBER, NOVEMBER -> Seasons.SPRING;
            case DECEMBER, JANUARY, FEBRUARY -> Seasons.WINTER;
            case JUNE, JULY, AUGUST -> Seasons.SUMMER;
            case MARCH, APRIL, MAY -> Seasons.AUTUMN;
        };
    }
over 4 years ago · Santiago Trujillo Report

0

This...

public class Seasons {
  enum Seasons {SPRING, SUMMER, WINTER, AUTUMN};

is giving me no end of issues, the compiler doesn't seem to like the fact that you've re-declared Seasons

You also seem to be missing an opportunity to encapsulate some of the functionality, for example, wouldn't it be easier if you could ask the MONTH directly what season it represents?

So, I started by renaming you enums so they are plural and then added support to the Month enum to get the current Season

public class Seasons {

    enum Season {
        SPRING, SUMMER, WINTER, AUTUMN
    };

    enum Month {
        JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
        SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;

        public Season getSeason() {
            switch (this) {
                case SEPTEMBER:
                case OCTOBER:
                case NOVEMBER:
                    return Season.SPRING;
                case DECEMBER:
                case JANUARY:
                case FEBRUARY:
                    return Season.WINTER;
                case JUNE:
                case JULY:
                case AUGUST:
                    return Season.SUMMER;
                case MARCH:
                case APRIL:
                case MAY:
                    return Season.AUTUMN;
            }
            return null;
        }
    };

    public static void main(String[] args) {
        System.out.println(Month.OCTOBER.getSeason());
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!