Tuesday 9 May 2017

Java Tutorial: Enum in java[How to define a constructor and method in enum - mobile]


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=w3j2BoswQgc&list=UUhwKlOVR041tngjerWxVccw

Mobile.java
enum Mobile
{
    Samsung(500), 
    Nokia(300), 
    Motorola(400);

    int price;

    Mobile(int price)
    {
        this.price = price;
    }

    int getPrice()
    {
        return price;
    }
}
EnumDemo.java
public class EnumDemo
{
    public static void main(String args[])
    {
        System.out.println("Mobile Phone List:");
        System.out.println("-------------------");
        
        Mobile[] mobileArray = Mobile.values();
        
        for (Mobile mobile : mobileArray)
        {
            System.out.println(mobile + " costs " + mobile.getPrice()
                    + " dollars");
        }
        System.out.println("-------------------");
        
        Mobile mobile = Mobile.Motorola;
        /*
         * Returns the name of this enum constant, exactly
         * as declared in its enum declaration.
         */
        
        String mobileName = mobile.name();      
        System.out.println("\nMobileName = " + mobileName);

    }
}
Output
Mobile Phone List:
-------------------
Samsung costs 500 dollars
Nokia costs 300 dollars
Motorola costs 400 dollars
-------------------

MobileName = Motorola
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/EnumDemo_mobile_Cons_method_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/EnumDemo_mobile_Cons_method_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/45d5519a1474198166ac515ccd559cc996bb550c/BasicJava/EnumDemo_mobile_Cons_method_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment