简体   繁体   中英

stackoverflow error in class constructor

Please excuse what is probably a very basic question, but I am writing a program to store employee info and it works fine until it tries to set the info inside my employee class. It gives a stackoverflow error and I cannot figure out why. Thanks for any help.

Main class:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        System.out.println("Enter the number of employees to enter.");
        int employeeCount = Input.nextInt();
        Input.nextLine();

        Employee employee[] = new Employee[employeeCount];
        String namesTemp;
        String streetTemp;
        String cityTemp;
        String stateTemp;
        String zipCodeTemp;
        String address;
        String dateOfHireTemp;

        for(int x = 0; x < employeeCount; x++)
        {
            System.out.println("Please enter the name of Employee " + (x + 1));
            namesTemp = Input.nextLine();
            System.out.println("Please enter the street for Employee " + (x + 1));
            streetTemp = Input.nextLine();
            System.out.println("Please enter the city of Employee " + (x + 1));
            cityTemp = Input.nextLine();
            System.out.println("Please enter the state of Employee " + (x + 1));
            stateTemp = Input.nextLine();
            System.out.println("Please enter the zip code of Employee " + (x + 1));
            zipCodeTemp = Input.nextLine();
            address = streetTemp + ", " + cityTemp + ", " + stateTemp + ", " + zipCodeTemp;
            System.out.println("Please enter the date of hire for Employee " + (x + 1));
            dateOfHireTemp = Input.nextLine();
            System.out.println("The employee ID for employee " + (x + 1) + " is " + (x + 1));
            employee[x] = new Employee(x, namesTemp, address, dateOfHireTemp);
        }
    }
}

Employee class:

public class Employee
{
    private int employeeID;
    private Name name;
    private Address address;
    private DateOfHire hireDate;

    public Employee()
    {

    }

    public Employee(int employeeID, String name, String address, String hireDate)
    {
        String temp;
        Name employeeName = new Name(name);
        this.employeeID = employeeID;
    }
}

Name class:

public class Name 
{
    public Name name;

    public Name(String name)
    {
        Name employeeName = new Name(name);
        this.name = employeeName;
    }
}

The most common cause of StackoverflowExceptions is to unknowingly have recursion, and is that happening here? ...

public Name(String name)
{
    Name employeeName = new Name(name);  // **** YIKES!! ***
    this.name = employeeName;
}

Bingo: recursion!

This constructor will create a new Name object whose constructor will create a new Name object whose constructor will... and thus you will keep creating new Name objects ad infinitum or until stack memory runs out. Solution: don't do this. Assign name to a String:

class Name {
    String name; // ***** String field!

    public Name(String name)
    {
        this.name = name;  // this.name is a String field
    }

Typically a class is used to group data together with functionality. It appears that the Name class is simply a wrapper for a String without adding any functionality. At this point in your Java career, it is probably better to declare String name; in the Employee class and remove the Name class all together. (Note that this would remove the error from your code that Hovercraft Full of Eels described.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM