Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Passing a class's object as parameter to a method...

  1. #1
    Join Date
    Apr 2010
    Location
    Warakapola, Sri Lanka
    Posts
    15
    Rep Power
    0

    Default Passing a class's object as parameter to a method...

    Hi,
    I wrote below code as my exercise. The exercise is to create a Purse.class and add coins to it with ArrayList. Then I need to transfer coins from one purse to another and append those coins using transfer(Purse other) method. The code is :
    Java Code:
    import java.util.ArrayList;
    
    
    public class Purse
    {
    	ArrayList<String> coins;
    
    
    	public Purse()
    	{
    		coins = new ArrayList<String> ();
    	}
    
    
    	public void addCoins(String coin)
    	{
    		coins.add(coin);
    	}
    	public void transfer(Purse other)
    	{
    		for(int i = 0; i < other.size(); i++)
    		{
    			coins.add(other.get(i));
    		}
    	}
    }
    Apparently, above code doesn't compile. The problem is with transfer() method. I have not any idea about passing an object as a parameter to a method inside the class that object belongs itself. Please help me to solve this problem.

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Passing a class's object as parameter to a method...

    If you have a question about a compilation error, it is most helpful to post the actual error message itself and not paraphrase it as often it has important details that are necessary for finding a solution. Also indicate by comment in the code the line(s) that cause the error.

    Edit: you're calling size() and get() methods on a Purse object. Does Purse in fact have these methods? If so you haven't shown them. Should you give your class these methods?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Passing a class's object as parameter to a method...

    ... or call those methods on the class member that does actually have those methods.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Passing a class's object as parameter to a method...

    True since you are calling methods of an object of the class from within the class, and so even if the fields are private, they will be visible within that method.

  5. #5
    melvin is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Re: Passing a class's object as parameter to a method...

    i'm a novice myself, however, size and get are methods of ArrayList; the objective here is to pass all the elements of a Purse (that is already defined to have only one ArrayList object as its attribute) and hence you must try 'other.coins.get() or other.coin.size(); like is said, i'm a enthusiast trying to learn, this just occurred to me and hence this post.

Similar Threads

  1. Object-Parameter passing costs
    By NewEonOrchestra in forum New To Java
    Replies: 1
    Last Post: 05-16-2012, 05:23 PM
  2. Replies: 2
    Last Post: 09-16-2011, 05:34 AM
  3. Class<T> in method parameter
    By Onra in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 12:12 AM
  4. Passing final parameter from a class
    By ianyappy in forum New To Java
    Replies: 8
    Last Post: 12-02-2010, 07:06 PM
  5. Replies: 10
    Last Post: 08-05-2010, 05:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •