Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a simple enum into a constructor in Java

I am trying to learn Java. I would like to have a enum as a parameter in the constructor. But I am getting an error.

public class Person {

    private int age, weight, height;
    private String name;

    private enum gender {MALE, FEMALE}

    public Person(int age, int weight, int height, String name, enum gender) {
         this.age    = age;
         this.weight = weight;
         this.height = height;
         this.name   = name;
         this.gender = gender;
    }
}

How would I handle the gender? I've tried with just gender and that didn't work either.

like image 312
kingcobra1986 Avatar asked Sep 23 '15 07:09

kingcobra1986


People also ask

Can we use enum in constructor Java?

Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.

How do you call an enum constructor?

Since the constructor is private , we cannot access it from outside the class. However, we can use enum constants to call the constructor. In the Main class, we assigned SMALL to an enum variable size . The constant SMALL then calls the constructor Size with string as an argument.

Can you inherit enums Java?

Java Enum and Interface As we have learned, we cannot inherit enum classes in Java. However, enum classes can implement interfaces.

Can enum type define constructors?

Because an enum is just another class type it can have constructors, fields and methods just like any other classes. Below we define a constructor that accept a string value of color code.


2 Answers

First, you need to create field of type gender...

private gender aGender;

Then you need to change the constructor to take a reference to an object of type gender

public Person(int age, int weight, int height, String name, gender aGender) {

Then you need to assign the parameter to your field...

this.aGender = aGender;

You gender enum should also be public

public enum gender {
    MALE, FEMALE
}

Otherwise, no one will be able to use it

For example...

public class Person {

    private int age, weight, height;
    private String name;
    private gender aGender;

    public enum gender {
        MALE, FEMALE
    }

    public Person(int age, int weight, int height, String name, gender aGender) {
                 this.age = age;
        this.weight = weight;
        this.height = height;
        this.name = name;
        this.aGender = aGender;
    }
}

You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others

like image 151
MadProgrammer Avatar answered Oct 05 '22 11:10

MadProgrammer


If you have enum Gender defined, you can directly pass Gender to constructor, change as

public Person(int age, int weight, int height, String name, Gender gender)
like image 31
Rahul Yadav Avatar answered Oct 05 '22 11:10

Rahul Yadav