Specifying array size with constructor


Specifying array size with constructor



I found some answers on this but I probably didn't understand it correctly because it didn't work for me



Employee is an object defined in another class


Employee


public class Firm {
Employee employees;

public Firm (int rosterSize){
this.employees = new employees[rosterSize];
}
}



Error: cannot find symbol, class employees



My question: how can I specify the size of the array employees with a constructor?





Is there a question here?
– Eran
Jul 2 at 13:50





If your class name is Employee, it should be new Employee[rosterSize]
– Eran
Jul 2 at 13:50


Employee


new Employee[rosterSize]





Welcome to StackOverflow! Please tell us more about the error that you are getting as your quellcode looks OK. Cheers :)
– vatbub
Jul 2 at 13:51





what's the question, I failed to understand, could you pleas elaborate?
– Nikhil Jain
Jul 2 at 13:51





Employee employees; makes no sense... Should be Employee employees; that is an array of Employee instances in a variable named employees...
– Usagi Miyamoto
Jul 2 at 13:56



Employee employees;


Employee employees;


Employee


employees




3 Answers
3



You need create Employee array, try this:


Employee


this.employees = new Employee[rosterSize];





I thought I tried that already but I must have made a typo before. case solved thanks.
– Daniel
Jul 2 at 13:54


public Firm (int rosterSize){
this.employees = new Employee[rosterSize]; // the class name is what you need
}



Thats simple. It has to be Object Name not its variable Name.


public class Firm {
Employee employees;

public Firm (int rosterSize){
this.employees = new Employee[rosterSize];
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages