
- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use enum constructor, instance variable & method in Java
Problem Description
How to use enum constructor, instance variable & method?
Solution
This example initializes enum using a costructor & getPrice() method & display values of enums.
enum Car { lamborghini(900),tata(2),audi(50),fiat(15),honda(12); private int price; Car(int p) { price = p; } int getPrice() { return price; } } public class Main { public static void main(String args[]){ System.out.println("All car prices:"); for (Car c : Car.values()) System.out.println( c + " costs " + c.getPrice() + " thousand dollars."); } }
Result
The above code sample will produce the following result.
All car prices: lamborghini costs 900 thousand dollars. tata costs 2 thousand dollars. audi costs 50 thousand dollars. fiat costs 15 thousand dollars. honda costs 12 thousand dollars.
java_methods.htm
Advertisements