简体   繁体   中英

Why won't this variable load?

The Short Story

I've created a program, below. I want to import a package I created into the program. I can't figure out why compiler doesn't recognize my address variable, whose class is located in a file under the package I created

address, name and date all share the same syntax, but the compiler doesn't recognize address. After removing address and executing the program with the remaining classes, I can successfully run my program. I can't do so with address, however.


Further Details

I created a program that prompts a user to enter the number of employees he or she wants to create, through the command line. A user then enters information about an employee's name, adddress and hire date and the program displays this information.

The code below DOES NOT CONTAIN classes for name, address and date, although I've referenced objects (and then fields) within those mentioned classes. Those classes have been saved in other files under one folder, because I'm trying to run my code as a package.

The folder's name is util -- hence the package name is util. Each source code begins with
package util;


import util.*;

public class EmployeeA
{
    Name name;
    Date date;
    Address address;

    public EmployeeA()
    {
        name = new Name();
        date = new Date();
        address = new Address();
    }

    public static void main(String[] args)
    {
        int x = Integer.parseInt(args[0]);
        EmployeeA[] array = new EmployeeA[x];

        for(int i = 0; i < x; i++)
        {
            array[i] = new EmployeeA();
            array[i].name.name = Input.getString("Enter employee first name and last name");
            array[i].date.date = Input.getString("Enter employee hire date in MM/DD/YYYY");
            array[i].address.address = Input.getString("Enter employee address");
        }
        for(int i = 0; i < x; i++)
            System.out.println(array[i].name.name + " was hired on " + array[i].date.date + " and lives on "
                    + array[i].address.address);

    }
}



                    }

source file #1

package util; 

public class Address
{
   String address;
}

source file #2

package util;
public class Date
{
    String date ;
}

source file #3

package util;

public class Name
{
    String name ;
}

employeeA.java:27: error: cannot find symbol

array [i].address.address = Input.getString("Enter employee address");
                 ^

symbol: variable address location: variable address of type Address

employeeA.java:31: error: cannot find symbol

System.out.println (array[i].name.name + " was hired on " + array[i].date.date + " and lives on " +  array[i].address.address);
                                                                                                                     ^

symbol: variable address location: variable address of type Address

Your address field within Address has no access modifiers - therefore it is only available to classes in the same package. Your EmployeeA class is not in the same package (it has no package statement), therefore it can't see it. You could make it public - but it would be better to make it private and add a method to access it ( getAddress() ). See the Java tutorial for more details.

Having said that, your existing types are pretty anaemic at the moment - and certainly there are better types for representing a date than String ...

it looks like you have an extra close curly bracket at the end of the EmployeeA class. Directly below the plus sign on the last line. I suspect that is causing it not to compile properly.

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