0

I have 3 classes: Company, Department, and Employee. The Company is made of Departments, and the Departments are made up of Employees. Pretty basic. But when I try to set each employee's Department, I get the following errors:

  • Syntax error on token "countingGuru", VariableDeclaratorId expected after this token
  • Syntax error on token(s), misplaced construct(s)

I have googled the errors, but I'm still having trouble figuring out what I did wrong.

Here is the code:

public class Company {
    static String[] validDeptNames = {
        "Accounting", "Human Resources", "Information Systems", "Marketing"
    };

    static Department accounting = new Department("Accounting");
    static Department marketing = new Department("Marketing");
    static Department infoSys = new Department("Information Systems");
    static Department humanRes = new Department("Human Resources");

    public static void main(String[] args) {

    }  
}

import java.util.ArrayList;

public class Department {
    Department department;

    ArrayList<Employee> employees;
    Department(String deptName){
        employees = new ArrayList<Employee>();

    }

    static Employee countingGuru = new Employee("Counting Guru", 55);
    static Employee countingPro = new Employee("Counting Pro", 45);
    static Employee countingSavvy = new Employee("Counting Savvy", 40);
    static Employee countingNovice = new Employee("Counting Novice", 25);
    static Employee salesGuru = new Employee("Sales Guru", 50);
    static Employee salesPro = new Employee("Sales Pro", 48);
    static Employee salesSavvy = new Employee("Sales Savvy", 38);
    static Employee hiringGuru = new Employee("Hiring Guru", 58);
    static Employee hiringPro = new Employee("Hiring Pro", 47);
    static Employee hackingPro = new Employee("Hacking Pro", 46);
    static Employee hackingGuru = new Employee("Hacking Guru", 51);
    static Employee hackingSavvy = new Employee("Hacking Savvy", 38);
    static Employee hackingNovice = new Employee("Hacking Novice", 23);

    public void addEmployee(Employee employee){
        employee.setDepartment(this);
        employees.add(employee);
    }

    accounting.addEmployee(countingGuru);

}
public class Employee implements Comparable<Employee> {
    String empName;
    int empAge;
    Department department;

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    String name;
    int age;
    public Employee(String name, int age) {
        this.name = name;
        this.age  = age;
    }

    public String getName() {
        return empName;
    }

    public void setName(String name) {
        this.empName = name;
    }

    public int getAge() {
        return empAge;
    }

    public void setAge(int age) {
        this.empAge = age;
    }

    @Override
    public int compareTo(Employee arg0) {
        return 0;
    }
}
9
  • Just a general note, you have fields with setters and getters but they are not declared private. Java fields are package-access by default unless explicitly declared otherwise. Some languages differ I know, notably Objective-C is the opposite (private by default). Having setters and getters for public/protected fields is of course redundant. Commented Oct 24, 2013 at 18:14
  • Your code is a total mess. You should read a tutorial first. Commented Oct 24, 2013 at 18:16
  • @Vash i think it's pretty clear i am just starting out. i first looked at java 6 days ago. i would love to make it not a "total mess," so could you post something constructive instead of being rude? thanks! Commented Oct 24, 2013 at 18:20
  • @thatpaintingelephant, I do not know why do you find my advice rude. I think that a tutorial is good place to start. Commented Oct 24, 2013 at 18:22
  • @thatpaintingelephant Not meant as a negative comment but if you are just starting out maybe you should master the basics before using stuff like Comparables and Collections. You can do those same things with conditional statements and arrays and the problems you will have will be much simpler to debug. Commented Oct 24, 2013 at 18:23

2 Answers 2

2

You have accounting.addEmployee(countingGuru); outside of any function. A non-variable declaration like this needs to be in a method, or a constructor, or a static block.

Since accounting is a static member of Company, you'll need to reference it as Company.accounting.addEmployee(countingGuru);.

Btw, this isn't great design to have so many things be static, because it means that every single Company object you ever create will have the same accounting, marketing, infoSys, and humanRes departments. You should make those fields non-static and initialize them in Company's constructor.

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

4 Comments

ahh, i see. so something like this? public void addToDepartment() { accounting.add(countingGuru); }
Because accounting and countingGuru are both static members it is also possible to enclose the statement in a static block: static { accounting.add(countingGuru); } but I don't recommend that. I agree with @musical_coder that so much use of static is unneeded. I point this out merely because you said you are learning and this is syntax you can do. (And there are some cases where a method call during static initialization is necessary.)
thank you. i hate to admit defeat, but i agree that i need to spend more time with the basics. i don't want to develop bad habits and throw myself into something i'm not prepared for.
@thatpaintingelephant: well said! Props to you for making the effort to learn this right.
1

Syntactically, if you change the constructor of Department to this

Department(String deptName){
    employees = new ArrayList<Employee>();
    Company.accounting.addEmployee(countingGuru);
}

and remove the line

    accounting.addEmployee(countingGuru);

then, your code will be error free.

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.