Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Methods In Different Classes

  1. #1
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Methods In Different Classes

    Hi, i have created a class for example People, and then another class PeopleList
    the PeopleList class uses the People Class to make an array list of the type people.

    in the People class i have a method:
    public String toString()
    {
       ..... code
    }

    from the PeopleList class I have done public ArrayList<People> myPeopleList = new ArrayList<People>();
    and i ccan type in myPeopleList. and the IDE brings up the available methods etc, and toString() is there so i can do myPeopleList.toString();

    In the People class i have now added another method:
    public String toMyFormat()
    {
       ..... code
    }
    but from the PeopleList class when i do myPeopleList. , the toMyFormat() method is not shown and i cannot use it.

    I dont understand why this is happening, could someone please explain?

    Thank You.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Methods In Different Classes

    The myPeopleList.toString() is NOT the method you have written in your People class. When you do myPeopleList.toString() you are calling the toString method for *the entire ArrayList*. To use the method for individual items in your ArrayList, you need to do myPeopleList.get(int index).toString() or myPeopleList.get(int index).toMyFormat(), so that you are calling first upon an element in the ArrayList, then the methods of that element.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Methods In Different Classes

    Hi, thanks, I understand now, the ToString method i had written was overriding the default ToString method.

    thanks once again.
    Last edited by Khadafi; January 11th, 2012 at 06:42 PM.

Similar Threads

  1. [SOLVED] Calling methods from other classes
    By knightknight in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 7th, 2011, 07:16 PM
  2. classes and methods??
    By paddy1 in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 10:02 AM
  3. [SOLVED] Help with Classes/ Instance Methods
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 9th, 2011, 06:33 PM
  4. [SOLVED] Help with classes & instance methods
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 16
    Last Post: July 30th, 2010, 02:20 PM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 07:55 AM