• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling a method from another class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
… and welcome to the Ranch
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic