Results 1 to 4 of 4
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default help me with this method please

    I didnt put all the code,, only the important parts
    If i have an array of objects, and i want to invoke a method from the class,
    Paycheck [] check = new Paycheck[2];
    check.totalGross(); // it will not work ..
    check[1].totalGross()// it will work
    Whats the difference??
    [code]
    import java.util.Date;


    //How to invoke the totalGross method ??
    //check.totalGross is wrong !!!! will not work
    //please help
    [code]
    Last edited by javanew; 03-28-2010 at 12:24 PM.

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

    Default

    You could do:

    Java Code:
    check[0].totalGross(check);
    ... or:

    Java Code:
    check[1].totalGross(check);
    ... or take any other arbitrary Paycheck object, but when you think of it it doesn't make any sense to have a Paycheck object handle an array of Paycheck objects; that method doesn't belong in the Paycheck class or maybe as a static method so you can do something like this:

    Java Code:
    Paycheck.totalGross(check);
    kind regards,

    Jos

  3. #3
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default

    So, do u think that i dont have to put it in the Paycheck ?
    i have to put it in Driver class ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javanew View Post
    So, do u think that i dont have to put it in the Paycheck ?
    i have to put it in Driver class ?
    It definitely doesn't belong in the Paycheck class as a non-static method. As I wrote: maybe you can put it there as a static method, but feel free to put it anywhere you want. Think of it: why should a single Paycheck object be able to handle an entire array of Paycheck objects?

    kind regards,

    Jos

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 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
  •