Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
J Kough
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
J Kough
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've also tried this, but it isn't working either. It is also not accessing the methods within the Address class.

Within Contact:

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Devarsh Shah
Greenhorn
Posts: 6
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can try it this way :


OR Else

 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic