java array type object of

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rajesh Shukla
    New Member
    • Aug 2007
    • 3

    java array type object of

    Array type object of a class is declared like abc a[10]=new abc(); but how to call methods
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Rajesh Shukla
    Array type object of a class is declared like abc a[10]=new abc(); but how to call methods
    That is not a valid initialization, nor assignment. The 'new abc()' expression
    results in a new object of type abc, not in an array type. btw, an array doesn't
    have methods either so you can't invoke them.

    Or maybe I misunderstood your question completely in which case you have
    to elaborate or rephrase a bit.

    kind regards,

    Jos

    Comment

    • Rajesh Shukla
      New Member
      • Aug 2007
      • 3

      #3
      Originally posted by JosAH
      That is not a valid initialization, nor assignment. The 'new abc()' expression
      results in a new object of type abc, not in an array type. btw, an array doesn't
      have methods either so you can't invoke them.

      Or maybe I misunderstood your question completely in which case you have
      to elaborate or rephrase a bit.

      kind regards,

      Jos
      i think it should be like
      abc a[]=new abc[10]; is it correct if yes then how to invoke a method

      Comment

      • Rajesh Shukla
        New Member
        • Aug 2007
        • 3

        #4
        i think it should be like
        abc a[]=new abc[10]; is it correct if yes then how to invoke a method

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Rajesh Shukla
          i think it should be like
          abc a[]=new abc[10]; is it correct if yes then how to invoke a method
          Yes, that is correct. That initialization sets up an array with ten reference to an
          abc object each. Note that there are no abc objects yet, the references don't
          'point' at such an object yet. You have to populate the array yourself as in:

          [code=java]
          abc[] a= new abc[10]; // create a new array
          for (int i= 0; i< a.length; i++) // populate the array
          a[i]= new abc();
          [/code]

          As I wrote above: you can't invoke methods on an array; you can invoke methods
          on an element of an array of course:

          [code=java]
          a[3].foo(); // assuming foo() is a method of class abc
          [/code]

          kind regards,

          Jos

          Comment

          Working...