enum example

Enum Example : Derive Class And Data

What is enum in java program?

Java language has many data types like primary data types, derive data types, user define data types.

Enum is one derive data types in java language.

Enum is also derive class, we can create derive class object and store multiple values in that derive object.

 

Let’s see enum example with Product class.

 

enum day  // Outside class declaration

{

                sun,mon,tue,wed,thr,fri,sat

                // enum derive object

}

class enum1

{

                // we can also declare enum inside the class

                enum Product

                {

                                mobile(10,5000),laptop(20,35000),comput(40,25000),mouse(40,650);

                                // this all enum data pass into enum constructor with quntity and price of product

 

                                int qnt;

                                int price;

                                Product(int qnt,int price) // Enum constructor

                                {

                                                this.qnt = qnt;

                                                this.price = price;

                                }

                }

                public static void main(String args[])

                {

                                // How to use in program?

                                System.out.println(day.tue);      // Output : wed

 

                                // How to use in Product base example???

                                System.out.println(“Product Name | Product Price | Product Qnt”);

                                Product p[] = Product.values(); // Store Enum object to  Array

                                for(Product prd : p)

                                {

                                                System.out.println(prd + “\t\t”+prd.price + “\t\t” + prd.qnt);

                                }

                }

}

Output:

Tue

Product Name | Product Price | Product Qnt

Mobile                  5000                       10

Laptop                  35000                    20

Comput                25000                    40

Mouse                  650                         40

 

 

Video available on YouTube.


If you have any query on blog example, you can directly comment us or subscribe our website

Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

250total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

EmailEmail
PrintPrint