Search code examples
javaarraysclassoopclass-design

Making a static reference to a non-static method in another class


I'm learning object-oriented programming in a Java course, and I'm doing a project where a program creates three types of objects: Address, Date, and Employee. The program stores data of several employees and then displays the data in an array of type Employee.

I'm using four different classes: an Address class, a Date class, and an Employee Class, and an EmployeeTest class that creates the array.

Here is the Address class:

public class Address {

    private String Street;
    private String City;
    private String State;
    private int ZipCode;

    public Address(String St, String Ci, String Sta, int Zip){
        Street = St;
        City = Ci;
        State = Sta;
        ZipCode = Zip;
    }

    public String getEmployeeAddress(){
        return (Street + ", " + City + ", " + State + " " + ZipCode);
    }
}

The Date Class:

public class Date {

    private int Month;
    private int Day;
    private int Year;

    public Date(int M, int D, int Y){
        Month = M;
        Day = D;
        Year = Y;
    }

    public String getDateString(){
        return (Month + "/" + Day + "/" + Year);
    }
}

And, the Employee Class:

public class Employee {
    private int EmployeeNum;

    public void setEmployeeNum(int ENum){
        EmployeeNum = ENum;
    }

    public int getNum(){
        return EmployeeNum;
    }

    public String getDate(){
        return Date.getDateString();
    }

    public String getName(){
        return Name.getEmployeeName();
    }
    public String getAddress(){
        return Address.getEmployeeAddress();
    }

}

All of these classes are in the same package (I'm using Eclipse). The point of the Employee class is to create an object of type Employee and be able to get it's Address, Name, and HireDate using the Address, Name, and Date classes.

The place where the array comes into play is here:

import java.util.Scanner;
import java.lang.*;

public class EmployeeTest {

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("How many employees will have their data stored today?");
        int EmployeeAmount = Integer.parseInt(input.nextLine());

        Employee [] EmployeeArray = new Employee[EmployeeAmount];

        for (int i = 0; i < EmployeeArray.length; i ++){
            System.out.print("What is employee " + (i+1) + "'s employee number?");
            int EmployeeNumber = Integer.parseInt(input.nextLine());
            EmployeeArray[i] =  new Employee();
            EmployeeArray[i].setEmployeeNum(EmployeeNumber);

            System.out.println("What is the first name of employee " + EmployeeNumber + "?");
            String EmployeeFirstName = input.nextLine();

            System.out.println("What is the last name of employee " + EmployeeNumber + "?");
            String EmployeeLastName = input.nextLine();

            Name EmployeeName = new Name(EmployeeFirstName, EmployeeLastName);

            System.out.println("Please enter the street address: ");
            String StreetAddress = input.nextLine();
            System.out.println("Please enter the name of the city: ");
            String CityName = input.nextLine();
            System.out.println("Please enter the two character code for the state: ");
            String StateID = input.nextLine();
            System.out.println("Please enter this address's zip code: ");
            int ZipCode = Integer.parseInt(input.nextLine());

            Address EmployeeAddress = new Address(StreetAddress, CityName, StateID, ZipCode);

            System.out.println("Finally, what was the month(#) of the hire date?");
            int Month = Integer.parseInt(input.nextLine());
            System.out.println("What was the day(#)?");
            int Day = Integer.parseInt(input.nextLine());
            System.out.println("What was the year?");
            int Year = Integer.parseInt(input.nextLine());

            Date HireDate = new Date(Month, Day, Year);



        }

        for (int j = 0; j < EmployeeArray.length; j ++){
            System.out.println("Employee number: " + EmployeeArray[j].getNum());
            System.out.println("Employee Name: " + EmployeeArray[j].getName());
            System.out.println("Employee Address: " + EmployeeArray[j].getAddress());
            System.out.println("Employee Hiredate: " + EmployeeArray[j].getDate());
        }


    }

}

The program prompts the user for the number of employees to be stored in the array, then creates an Employee[] of size EmployeeAmount. The idea of the code is that for each Employee in the Array, all of the variables in the other classes are obtained: Employee Number, Employee Name (first and last), Address (Street Address, City, State Code, Zip Code), Hire Date (Month, Day, Year). After all this is obtained, the second for loop iterates through each Employee and displays the info.

The problem I am having is that in the Employeeclass, Eclipse gives me an error in the getDate(), getName(), and getAddress() methods. When I say for example, return Date.getDateString(), Eclipse says that I cannot make a static reference to a non-static method. It's solution is to make getDateString() static, and I tried this, but the problem is that by making all the methods and variables in the Address, Employee, and Date classes, the values are locked. Meaning that the same data will be displayed for all employees.

Here's what I mean. Below is a sample output if I made all the methods and variables static. The text in between the asterisks is what the user inputs.

How many employees will have their data stored today?**2** What is employee 1's employee number?**1** What is the first name of employee 1? **Bob** What is the last name of employee 1? **Jones** Please enter the street address: **300 1st Avenue** Please enter the name of the city: **New York** Please enter the two character code for the state: **NY** Please enter this address's zip code: **10001** Finally, what was the month(#) of the hire date? **1** What was the day(#)? **1** What was the year? **2001** What is employee 2's employee number?**2** What is the first name of employee 2? **Bobby** What is the last name of employee 2? **Robinson** Please enter the street address: **301 1st Avenue** Please enter the name of the city: **Los Angeles** Please enter the two character code for the state: **CA** Please enter this address's zip code: **90001** Finally, what was the month(#) of the hire date? **1** What was the day(#)? **2** What was the year? **2004** Employee number: 2 Employee Name: Bobby Robinson Employee Address: 301 1st Avenue, Los Angeles, CA 90001 Employee Hiredate: 1/2/2004 Employee number: 2 Employee Name: Bobby Robinson Employee Address: 301 1st Avenue, Los Angeles, CA 90001 Employee Hiredate: 1/2/2004

By making all of the variables and methods static, the values are locked as shown, which makes the program useless. Does anyone have a solution to this problem? I need a way to display the information of each employee while referencing the methods in the other classes. Now, normally I would just create all the variables and methods under one class called Employee, but the assignment instructions specify that I need to make individual classes.


Solution

  • You are creating a Name, Address, and Date for each iteration of the for loop. But you have not way, nor do you set them on each Employee instance.

    You need to add methods to set the values on each Employee and store the information. Something like this:

    public class Employee {
        private int employeeNum;
        private Name name;
        private Date hireDate;
        private Address address;
    
    
        public void setEmployeeNum(int eNum){
            employeeNum = eNum;
        }
    
        public int getEmployeeNum(){
            return employeeNum;
        }
    
        public void setHireDate(Date date){
            this.hireDate = date;
        }
    
        public String getHireDate(){
            return hireDate.getDateString();
        }
    
        public void setName(Name n){
            this.name = n;
        }
    
        public String getName(){
            return name.getEmployeeName();
        }
    
        public void setAddress(Address addy){
            this.address = addy;
        }
    
        public String getAddress(){
            return address.getEmployeeAddress();
        }
    }
    

    Then in your for loop, set the values:

      EmployeeArray[i].setName(EmployeeName);
      EmployeeArray[i].setAddress(EmployeeAddress);
      EmployeeArray[i].setHireDate(HireDate);
    

    By the way, you should not capitalize variables, only classes. Variables should be camel-cased.