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 4 of 4

Thread: Return a List from a method

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Return a List from a method

    Hello!

    I have created a list of customer in a class called bank like this:

     private List<customer> customerList = new LinkedList<>();
     
        public List getCustomerList()
        {
            return customerList;
        }

    Then I´ve added customers to that list via another test class. Now I want to print out the customer list via a for-loop in that class:

    for (Customer c : bank.getCustomerList())
                {
                    code for print out
                }

    I then get an error message saying: "Incompatible datatypes, Required Customer, Found: Object"

    Why is that? I know that I can just declare the list as public and then call the variable directly, but I wanna know why the other version doesn't work.

    Thanks!

    /Hank


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Return a List from a method

    Please post the full text of the error message showing the statement where the error happens.

    The returned List is not defined to be a list of Customers. Look at how customerList is defined.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Return a List from a method

    You have not used Generics in the return type. Therefore the List is full of Object objects and not Customer objects.
    Improving the world one idiot at a time!

  4. The Following User Says Thank You to Junky For This Useful Post:

    iHank (October 30th, 2013)

  5. #4
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Return a List from a method

    Quote Originally Posted by Junky View Post
    You have not used Generics in the return type. Therefore the List is full of Object objects and not Customer objects.
    Thank you! That worked

Similar Threads

  1. breaking a method before having a return value
    By Wolfone in forum Exceptions
    Replies: 3
    Last Post: September 6th, 2013, 07:19 AM
  2. return from a method
    By MHS in forum Java Theory & Questions
    Replies: 1
    Last Post: June 15th, 2013, 01:06 PM
  3. How do I return an array list?
    By 93tomh in forum Java Theory & Questions
    Replies: 2
    Last Post: July 30th, 2012, 04:03 PM
  4. Why I can't return two variables in one method?
    By netlync in forum Java Theory & Questions
    Replies: 3
    Last Post: January 13th, 2012, 02:39 AM
  5. Method return value(s)
    By mwr76 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2011, 02:38 PM