Results 1 to 4 of 4
  1. #1
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default Array as object or param?

    Hi!

    If I have an array of objects, lets say NULL Object in this example. Is it possible to call actions, or methods if you will, upon it using this syntax: objects.method().

    I tried to populate the array using different methods. I couldn't get objestc.method() working. Instead I had to do it like this, passing the array as a param:

    Java Code:
    populate(arrayOfTypeObject);  //usage of method "on" array.
    How should I go about this?

    Edit, I think I got it working if I looped through the array.
    Java Code:
    for(loop with index i)
    object[i].populate();
    But I preferred a method iterating instead of iterating a method...

    Edit2, comments of this please... :-)
    Last edited by überfuzz; 02-08-2011 at 02:07 PM. Reason: Comment

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    So Maybe I not correct understand question.
    No you can't do it. because compiler need not null reference on the object that invoke anything methods.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Some languages provide a syntax that lets you take an array and do things to each of its elements. Something along the lines of:

    arrayOfTypeObject.each(populate);

    Java doesn't do this: arrays have very limited behaviour, and there is no type corresponding to the sort of thing populate would have to be.

    As you have found you have to loop through the array, either using an index or with an enhanced for loop:

    Java Code:
    for(SomeClass sc :arrayOfTypeObject) {
        sc.populate();
    }

    (I am assuming the array is declared to actually contain things that implement populate(). An array of Object is a poor choice of array because Object also has very little behaviour.)

    --------------------------

    As far as null is concerned, you generally have to keep in mind that two things can be null: the variable representing the array AND a variable or expression representing the object at a given index. You can't use an expression like arr[i] unless you are sure that arr is not null, and you can't use a expression like arr[i].populate() unless you are sure that arr[i] is not null.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    So yeah, you def. need to check for null. However, if you have a mixed bag of types and need to call the same method on all of them, you can use an interface to allow this behavior between mixed types.

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  2. How to use <s:param> tag
    By aruna.hcu in forum Web Frameworks
    Replies: 7
    Last Post: 01-13-2010, 09:25 AM
  3. Parsing & handling of <object> and <param> tags
    By Floxxx in forum AWT / Swing
    Replies: 1
    Last Post: 12-29-2009, 04:32 PM
  4. Javadoc - cannot compile @param tags
    By jon80 in forum New To Java
    Replies: 9
    Last Post: 05-14-2009, 09:57 AM
  5. jsp:param action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 11:03 AM

Posting Permissions

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