aspose file tools*
The moose likes Beginning Java and the fly likes Calling a method from within a method in another class (in the same package)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling a method from within a method in another class (in the same package)?" Watch "Calling a method from within a method in another class (in the same package)?" New topic
Author

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

J Kough
Greenhorn

Joined: Mar 03, 2012
Posts: 2
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

Joined: Mar 03, 2012
Posts: 2
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Mar 02, 2012
Posts: 6

Well you can try it this way :


OR Else

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Mar 17, 2011
Posts: 7015
    
  16

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

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a method from within a method in another class (in the same package)?
 
Similar Threads
Interfaces and abstract classes
So exhausted...anyone there to help?
call second constructor?
Hash code consistency across multiple implementations.
Java dunce is back...still can't figure this out