5 Replies - 262 Views - Last Post: 19 May 2014 - 05:57 AM Rate Topic: -----

#1 alfonsP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-May 14

Using an Enumeration as a parameter

Posted 18 May 2014 - 09:52 AM

Hey guys, so I am trying to create a method called getVariable will has a parameter of a variable that has a type of Test, an enumeration. An int variable called x is assigned a value of 5, and through control statements, I want its value to change depending on what the argument is. I was able to correctly create this method, but when I use it I get an error saying, "non static method getVariable(Test) cannot be referenced from a static context" on line 28. I am just trying to learn how to use enumerations, so I was appreciate advice on how to fix this.

Here is the code:

package javaapplication5;

public class Product implements Cloneable
{
    public enum Test
    {
        VARAIABLE_ONE,
        VARAIABLE_TWO,
        VARIABLE_THREE
    }
    
    private String product;
    private String description;
    private double price;
    private Test variable;
    
    public Product(String product, String description, double price)
    {
        this.variable = Test.VARIABLE_THREE;
        this.product = product;
        this.description = description;
        this.price = price;
    }
    
    public static void main(String[] args)
    {
        Product product = new Product("Java", "A java book", 49.99);
        System.out.println(getVariable(product.variable));
    }
    
      
    public int getVariable(Test variable)
    {
        int x = 5;
        
        if(variable == Test.VARAIABLE_ONE)
            x = 1;
        if(variable == Test.VARAIABLE_TWO)
            x = 2;
        if(variable == Test.VARIABLE_THREE)
            x = 3;
        
        return x;
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Using an Enumeration as a parameter

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 686
  • View blog
  • Posts: 2,985
  • Joined: 03-December 12

Re: Using an Enumeration as a parameter

Posted 18 May 2014 - 09:59 AM

You are attempting to call a static method within its own class. To remove the error you can add static to the method you are attempting to call.
Was This Post Helpful? 1
  • +
  • -

#3 alfonsP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-May 14

Re: Using an Enumeration as a parameter

Posted 18 May 2014 - 10:04 AM

View Postastonecipher, on 18 May 2014 - 09:59 AM, said:

You are attempting to call a static method within its own class. To remove the error you can add static to the method you are attempting to call.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Using an Enumeration as a parameter

Posted 18 May 2014 - 12:03 PM

fyi an 'enumeration' in Java is more likely to mean java,util.Enumeration. The term you're looking for is enum or, if you want to communicate it in a generic way, 'enumerated type'

This post has been edited by g00se: 18 May 2014 - 12:04 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#5 Zavael  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 13-May 14

Re: Using an Enumeration as a parameter

Posted 19 May 2014 - 05:42 AM

I wouldnt say that setting the getVariable method as static is good. I would change the output instead to
System.out.println(product.getVariable(product.variable));

in this way you do not need to set the method as static.

On the other hand, you do not need to pass the product.variable to the method, as it is the instance method of the object Product. you can access the variable from the method inside like this:
    public int getVariable() // <- there is no parameter!
    {
        int x = 5;
        
        if(variable == Test.VARAIABLE_ONE) // your variable is accessible from here
            x = 1;
        if(variable == Test.VARAIABLE_TWO)
            x = 2;
        if(variable == Test.VARIABLE_THREE)
            x = 3;
        
        return x;
    }

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5835
  • View blog
  • Posts: 12,689
  • Joined: 16-October 07

Re: Using an Enumeration as a parameter

Posted 19 May 2014 - 05:57 AM

Actually, if you're going to use an enum and want some numeric value assocated with it, you should probably make it part of the enum.

e.g.
class Product implements Cloneable {
    public enum Test {
        VARAIABLE_ONE(1), VARAIABLE_TWO(2), VARIABLE_THREE(3);
        private final int value;
        Test(int value) { this.value = value; }
        public int getValue() { return this.value; }
    }

    private String product;
    private String description;
    private double price;
    private Test test;

    public Product(String product, String description, double price) {
        this.test = Test.VARIABLE_THREE;
        this.product = product;
        this.description = description;
        this.price = price;
    }


    public Test getTest() { return this.test; }
    
    public int getTestValue() { return this.test.getValue(); }

    public static void main(String[] args) {
        Product product = new Product("Java", "A java book", 49.99);
        System.out.println(product.getTestValue());
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1