0

Here's my code, I'm trying to access e1,e2,e3 in my getData() method but it's not working. How do I get my second method to recognize the first ones but keeping the methods separate?

public class Model {

    public Model() {
        loadData();
    }

    public void loadData() { 
        Employee e1 = new Employee("John Smith", "California, PA.", "Dr. Robertson junior.");

        Employee e2 = new Employee("Kyle Alston",  "State College, PA.", "SmithTown");

        Employee e3 = new Employee("Tommy Smith", "Baskeville, PA.", "Chicago");
        
      
    }
    public String getData(int n)  { 
        if(n == 1){
            return e1;

        }
        if(n == 2){
            return e2;

    }
        if(n == 3){
            return e3;

    }
        
        
        
    }
}

I tried Model data = new loadData().e1 but I don't think that works.

Thanks in advance. I'm not allowed to change the parameters.

6
  • 2
    You need to understand 'scope'. The variables e1, e2, e3 simply do not exist when loadData is not being executed. You should declare them as instance (member) variables. Having said that, the code would be more succinct using an array. Commented Aug 31, 2021 at 0:03
  • @user16632363 so there's no way to access them? Commented Aug 31, 2021 at 0:05
  • You can't access something that does not exist, The variables come into existence when loadData is entered, and cease to exist just prior to return from loadData. Commented Aug 31, 2021 at 0:05
  • @user16632363 does that mean my loadData() method is useless? Commented Aug 31, 2021 at 0:06
  • Your loadData() method creates employees that all get lost when the method finishes executing. Commented Aug 31, 2021 at 0:07

1 Answer 1

4

Use member variables to retain values from one method to the next.

public class Model {

    private Employee e1, e2, e3;

    public Model() {
        loadData();
    }

    public void loadData() { 
        e1 = new Employee("John Smith", "California, PA.", "Dr. Robertson junior.");
        e2 = new Employee("Kyle Alston",  "State College, PA.", "SmithTown");
        e3 = new Employee("Tommy Smith", "Baskeville, PA.", "Chicago");    
    }

    public String getData(int n)  { 
        if (n == 1) {
            return e1;
        }
        if (n == 2) {
            return e2;
        }
        if (n == 3) {
            return e3;
        }
        return null;
    }
}

For comparison, rewritten using a simple array. The benefit here is that getData is now independent of the number of employees.

public class Model {

    private Employee[] e;

    public Model() {
        loadData();
    }

    public void loadData() { 
        e = new Employee[3];
        e[0] = new Employee("John Smith", "California, PA.", "Dr. Robertson junior.");
        e[1] = new Employee("Kyle Alston",  "State College, PA.", "SmithTown");
        e[2] = new Employee("Tommy Smith", "Baskeville, PA.", "Chicago");    
    }

    public String getData(int n)  { 
        if (n <= 0 || n > e.length) {
           return null;
        }
        return e[n-1];
    }
}

I might consider using an ArrayList rather than an array, but the array is more fundamental in Java, which I think makes it a better choice for initial learning.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.