DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > A New JEP to Enhance Java Enums [Code Snippet]

A New JEP to Enhance Java Enums [Code Snippet]

Enums a versatile and useful, but they currently don't support generics. One JEP has proposed fixing that, providing even greater use for enums.

A N M Bazlur Rahman user avatar by
A N M Bazlur Rahman
CORE ·
Dec. 16, 16 · Java Zone · News
Like (2)
Save
Tweet
10.03K Views

Join the DZone community and get the full member experience.

Join For Free

Enum is one of the strongest features in Java. They are used as a list of constraints. An enum can contain a constructor, a method, a field etc. This link goes into some more details, but we're going to cover one important aspect of them here. 

They don't support generics.

Let's check the following code:

public enum Cars {
    AUDI(new Audi()),
    ACURA(new Acura());

    private Vehicle vehicle;

    Cars(Vehicle vehicle) {
        this.vehicle = vehicle;
    }
    public Vehicle getVehicle() {
        return vehicle;
    }
}

public abstract class Vehicle {
}

public class Acura extends Vehicle {
    public int getSerialNo() {
        return 134;
    }
}

public class Audi extends Vehicle{
    public String getName() {
        return "Audio";
    }
}


We have two different methods in the two classes. Now we can access the enum using the following:

public class Main {
    public static void main(String[] args) {
        Vehicle vehicle = Cars.ACURA.getVehicle();
    }
}


But there is no way we can call the getSerialNo() method from this.

To solve this problem, a new JEP (JDK Enhancement Proposal) has submitted.

With this JEP, we will able to do following:

public enum Cars<T extends Vehicle> {
    AUDI<Audi>(new Audi()),
    ACURA<Acura>(new Acura());

    private T vehicle;

    Cars(T vehicle) {
        this.vehicle = vehicle;
    }

    public T getVehicle() {
        return vehicle;
    }
}


And, definitely, we will able to call the getSerialNo() method like following:

Cars.ACURA.getVehicle().getSerialNo();


Java (programming language) Snippet (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Handling Multiple Browser Windows and Tabs in Selenium PHP
  • Testing Your Infrastructure as Code Using Terratest
  • Progressive Delivery With Argo Rollouts: Blue-Green Deployment
  • Common Types Of Network Security Vulnerabilities In 2022

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo