Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: Creating an object

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Creating an object

    Java Code:
    ListView listView= (ListView) findViewById(R.id.listview)
    It would be understandable for me if it was something like
    Java Code:
    ListView listView = new ListView()
    But I don't understand what the RHS of ListView listView = (ListView) findViewById(R.id.listview) means; I know that LHS creates a reference variable named listView which will contain the reference to an object of ListView.

    To the best of my understanding, is it retrieving a view by findViewById() and parsing to a ListView object (how can an object of one type be even parsed into an object of another type) , and then assigning a reference to that ListView object in listView reference variable?
    Thank you in advance.
    Last edited by Nazneen Ali; 10-27-2013 at 06:25 PM.
    Don't forget to smile :-)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: Creating an object

    It's not 'parsing'; it's 'casting'. Casting in Java means 'reinterpreting' as in B extends A, so a B can be cast to an A. Also, if something is a B already but you only know it's an A, you can cast the object to B again. The first form is 'upcasting' and it always succeeds; the second form is 'downcasting' and it might fail. Obviously, whatever your method returns, it can be cast (up or downwards) to a ListView (otherwise an Exception is raised).

    kind regards,

    Jos
    Nazneen Ali likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Creating an object

    Thank you.
    Don't forget to smile :-)

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 01:50 PM
  2. Replies: 9
    Last Post: 07-29-2011, 02:18 PM
  3. Creating a cylinder object
    By b_erten in forum New To Java
    Replies: 8
    Last Post: 07-25-2010, 08:43 PM
  4. Creating an object and getting a value from it
    By BrownBread in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 06:08 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

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