Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6542295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:12:32+00:00 2026-05-25T11:12:32+00:00

First class which got the enum: public class EnumTest { private Employee empl; public

  • 0

First class which got the enum:

public class EnumTest {

    private Employee empl;

    public EnumTest(Employee empl) {
        this.empl = empl;
    }

    public enum ALL_STRING {

        FNAME(empl.getFirstName()),
        LNAME(empl.getLastName()),
        POSITION(empl.getPosition());
        String str;

        ALL_STRING(String inStr) {
            str = inStr;
        }

        public String getStr() {
            return str;
        }
    }
}

Employee class:

public class Employee {

    private String firstName;
    private String lastName;
    private String position;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
}

When I am trying to compile it it gets error that,

/home/shamal/src/Test/src/test/EnumTest.java:21: non-static variable empl cannot be referenced from a static context FNAME (empl.getFirstName()),

/home/shamal/src/Test/src/test/EnumTest.java:22: non-static variable empl cannot be referenced from a static context LNAME (empl.getLastName()),

/home/shamal/src/Test/src/test/EnumTest.java:23: non-static variable empl cannot be referenced from a static context POSITION (empl.getPosition());

3 errors

What is the wrong on the code and what should be the correct way?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:12:32+00:00Added an answer on May 25, 2026 at 11:12 am

    Enums are statically (compile time) defined, unique objects, so you can’t create their values using dynamic parameters.

    The direct problem is that you are trying to instantiate the enum using a nonstatic member variable, which is thus not available at the time of instantiating the class itself – it will only be initialized when a new class object is created. However, all enum values must be fully initialized at the time the enclosing class is initialized.

    It is hard to see what you are actually trying to achieve with this code – maybe if you explain it in words, we can offer a better alternative approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which gets called first - the base constructor or other stuff here? public class
I've got following factory class which has a dependency $client. I moved the dependency
I got 2 classes in my project. The first class(main) have a listview and
I'm creating my first class, mainly guided by Overland's C++ Without Fear. I've made
When are objects or something else said to be "first-class" in a given programming
First off, I understand the reasons why an interface or abstract class (in the
I often refactor code first by creating an inner class inside the class I'm
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)
First, I know there are methods off of the generic List<> class already in
In my assembly language class, our first assignment was to write a program to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.