Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How is Enum interpreted here?

    Hi, recently I have come across this class diagram regarding numbers and there's this part about the numbers class that I don't understand at all which I would like insight from.



    As the picture above, how does the value val come into question in this case and how should I interpret it in a coding way?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I've never seen an enum actually written out in a uml style before. If I had to guess at it, this is what it represents:
    PHP Code:

    public enum Number
    {
        
    ONE(1),
        
    TWO(2),
        
    THREE(3);
        
        private final 
    int val;
        
        private 
    Number(int val)
        {
            
    this.val val;
        }
        
        public 
    int getVal()
        {
            return 
    this.val;
        }

    The val comes to being the actual value associated with the enum number. It could be any datatype you want and can be called just like:
    PHP Code:
    Number one Number.ONE;
    System.out.println(one.getVal()); 
    So if getVal() where to return a string, that can be done and replace the "one" value during the "construction" phase of the enum.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    I've never seen an enum actually written out in a uml style before. If I had to guess at it, this is what it represents:
    PHP Code:

    public enum Number
    {
        
    ONE(1),
        
    TWO(2),
        
    THREE(3);
        
        private final 
    int val;
        
        private 
    Number(int val)
        {
            
    this.val val;
        }
        
        public 
    int getVal()
        {
            return 
    this.val;
        }

    The val comes to being the actual value associated with the enum number. It could be any datatype you want and can be called just like:
    PHP Code:
    Number one Number.ONE;
    System.out.println(one.getVal()); 
    So if getVal() where to return a string, that can be done and replace the "one" value during the "construction" phase of the enum.
    Hi thanks for the reply, I appreciate it alot!

    I got a question though, in the 2nd example you gave, does that mean that the val of one.getVal would be 1, since it is associated with the enum of ONE?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That's correct, that's defined here:
    PHP Code:
        ONE(1), 
    In the enum block. It literally constructs an instance of itself, and allows you to operate on it since the Number enum datatype includes the getVal method.
    You can add as many methods as you wish, much like any standard class object. The only real difference is that the enums are enumerated types, so it MUST be one of the options it provides, while the class instantiation doesn't have that limitation.
    Also, unlike a class, enums cannot be instanated with the new keyword at all (the enum override forbids the use of public on a constructor scope). This also means that via reflection, it will fail to invoke on a call to .newInstance() since that would be defined as an InstantiationException.

    One of the most useful features of the enum in java is that you can switch on an enum. This is especially important for people like me, who are used to being able to switch on strings (java does not allow you to switch on anything non-primitive EXCEPT for enum). It is slightly more bizarre though:
    PHP Code:
    Number n Number.THREE;
    switch (
    n)
    {
        case 
    THREE:
            
    System.out.println("Its a three.");
            break;

    Note that its not cased on Number.THREE, rather just THREE as its capable of picking out the scope as being that of Number.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    PaulAsbome (06-15-2013)

  • #5
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Also, unlike a class, enums cannot be instanated with the new keyword at all (the enum override forbids the use of public on a constructor scope). This also means that via reflection, it will fail to invoke on a call to .newInstance() since that would be defined as an InstantiationException.
    Thanks for your more detailed explanation about enum and I am actually awed by how powerful enum is on the programming aspect.However, what I don't understand is the part on Instantiation. What does that exactly mean? Does that mean that I am not allowed to do something like this?

    Using the previous Number Enum as example:

    PHP Code:

    Number Number1 
    = new Number("1"

  • #6
    New Coder
    Join Date
    Aug 2014
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
    Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum.

  • #7
    New to the CF scene
    Join Date
    Sep 2014
    Location
    pakistan
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Web Hosting Service

    We need to inform you that the MySQL service is temporarily unavailable on the server.

    We are investigating the reasons of the situation and already processing maintenance on this matter.

    Estimated Service Restoration: Unfortunately, no ETA can be currently provided. We will update you as soon as any new information is available. Please check for the updates in the comments of this post.

    We apologize for any inconvenience this may be causing you or your company. Your patience and understanding are highly appreciated.skyhost.pk


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •