Forums Register Login

Calling a method from within a method in another class (in the same package)?

+Pie Number of slices to send: Send
I hope I can explain this... I'm in my first semester of Java courses, so I'm still pretty new at this.

This is what I'm trying to do, if there's a better way to do it please let me know:

I have created a class named Contact. Within the Contact class are fields String contactName, Address contactAddress, String contactTelephone, and String contactEmail.

I also have a class named Address. The Address class contains the String fields streetAddress, city, and postalCode.

The contactAddress within Contact uses the data type Address, as shown above.

I have mutator methods within Address, for each field.

I have a mutator method within Contact that accepts an Address variable:


I would like to overload this, and create a mutator method that accepts three Strings, for the streetAddress, city, and postalCode.

This is what I'm trying to add within the Contact class, but it's not working:


I can't seem to access the public method setAddress (from the Address class) from within a method in the Contact class. Is this possible?

Or how else might I be able to accomplish this?
+Pie Number of slices to send: Send
I've also tried this, but it isn't working either. It is also not accessing the methods within the Address class.

Within Contact:

+Pie Number of slices to send: Send
Welcome to the Ranch!

When you're in the Contact class, as you are in the examples you've posted, this refers to the instance of the Contact class you're in at the time. So, for example, this.setAddress(street, city, zip) is trying to call setAddress(street, city, zip) on the Contact object. Which doesn't have that method.

You want to call it on the Address object instead. That means you have to call it via Address object reference - contactAddress, in this case.

Alternatively, you could create a new Address object and change contactAddress to point to it.
+Pie Number of slices to send: Send
Well you can try it this way :


OR Else

+Pie Number of slices to send: Send
It would be far better to make sure contactAddress is initialised in the constructor. Make sure to get rid of any constructors which don’t initialise it.
+Pie Number of slices to send: Send
 

J Kough wrote:I would like to overload this, and create a mutator method that accepts three Strings, for the streetAddress, city, and postalCode.


I'm not quite sure why. All it saves you is a few characters when calling, viz:
setContactAddress(street, city, zip);
as opposed to:
setContactAddress(new Address(street, city, zip));

This is what I'm trying to add within the Contact class, but it's not working:I can't seem to access the public method setAddress (from the Address class) from within a method in the Contact class. Is this possible?
Or how else might I be able to accomplish this?


If you really feel you have to, I'd do:but, as I say, it seems a bit redundant to me.

Clean APIs generally don't have a lot of overloaded methods, and you can easily get sidetracked from your primary goal by creating lots of "convenience" methods.

My advice: keep to methods that are strictly required for the business logic for the moment.

Winston
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2677 times.
Similar Threads
What does class or interface expected error mean?
call second constructor?
Java dunce is back...still can't figure this out
Hash code consistency across multiple implementations.
Interfaces and abstract classes
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:18:46.