• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a method from another class  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problem to call a method from another class using arrays. I already know how to call a method without using arrays but I am not sure how to pass parameters to the main method using arrays. I'd really appreciate any feedback or comments!

This is what I have so far.


 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the Customer.data(...) method is an instance method, so you would need to create a Customer first in order to call it. But since the purpose of the method is to generate customers to fill the array, having to make a Customer to do so doesn't make sense. In this case, since your data() method is what is called a 'factory method' (a method which makes objects), you should make it static. Then you could call it from the Customer class like this:

I think you need some work on your variable names, though. For example, x isn't very expressive, a better name might be numberOfCustomers. Also, the variable person is singular, but is an array - which typically holds multiple instances. It also doesn't really reflect the contents (which are instances of Customer, not instances of Person). So maybe a better name might be customers which indicates both plurality and what it holds. Finally, the method data() isn't expressive either, since it doesn't really indicate what action the method is doing. Perhaps getCustomersFromInput() or something like that would be better. These are just ideas, of course. The purpose is that you can be pretty expressive with the names of variables and methods to make it more obvious what they are used for.
 
Sophia Gil
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:the Customer.data(...) method is an instance method, so you would need to create a Customer first in order to call it. But since the purpose of the method is to generate customers to fill the array, having to make a Customer to do so doesn't make sense. In this case, since your data() method is what is called a 'factory method' (a method which makes objects), you should make it static. Then you could call it from the Customer class like this:

I think you need some work on your variable names, though. For example, x isn't very expressive, a better name might be numberOfCustomers. Also, the variable person is singular, but is an array - which typically holds multiple instances. It also doesn't really reflect the contents (which are instances of Customer, not instances of Person). So maybe a better name might be customers which indicates both plurality and what it holds. Finally, the method data() isn't expressive either, since it doesn't really indicate what action the method is doing. Perhaps getCustomersFromInput() or something like that would be better. These are just ideas, of course. The purpose is that you can be pretty expressive with the names of variables and methods to make it more obvious what they are used for.


Thank you Steve for your feedback! I tried to make the method static but it shows me a message that says " The method data cannot be declared static; static methods can only be declared in a static or top level type." Then for each of the input statements of the method such as name, age, address, and gender, it shows the message "Cannot make a static reference to the non-static field name/age/address/gender."

I am sorry, I am new to Java. I apologize for my limited understanding of Java.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and welcome to the Ranch
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!