Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9250295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:24:48+00:00 2026-06-18T10:24:48+00:00

I have an array with objects inside. How would I use methods on the

  • 0

I have an array with objects inside. How would I use methods on the array element?

My array is of type Object. I tried array[i].getSalary() but this doesn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:24:49+00:00Added an answer on June 18, 2026 at 10:24 am

    First of all, you should almost never use an array of Object. The reason is that you lose all type information that :

    • would make the code more understandable for people who will read your code (including your professor, yourself, and yourself in six months when you will have forgotten everything about the code)
    • could be used by the compiler and your IDE to tell you when you are doing mistakes even before you test the program.
    • would avoid the type of error-prone casting that I am going to explain to you in the second part of this answer.

    Instead of an array of Object, use an array of the type corresponding to the types of the object that will be put inside it (or a common base class). Assuming that your class is called Employee, you should declare your array this way:

    Employee[] employeeArray;
    

    (employeeArray is a better name than array because it tells what kind of objects it contains, again for readability. In general, prefer explicit names for variables.)

    With that solution it is easy to use employeeArray[i].getSalary(), if the class Employee contains such method. The intention of this code is also obvious when you read it.

    Other possibilities are generic collections like List<Employee> or Set<Employee>, depending on your needs.

    If you really have to use an array of Object, and call a getSalary() method, you will have to cast the array elements to the class or interface to which the method getSalary() belongs.

    For example and again, if this class is called Employee:

    Employee employee = (Employee) array[i];
    employee.getSalary();
    

    What casting does is obtaining a reference of type Employee of your object. The object is still the same but now you can call methods of Employee on this object.

    But this solution have a number of caveats. First, it is more verbose and it takes two lines to make what could have taken just one. Second, and more importantly, since you have an array of Object, you can not be certain that you really have an object of type Employee, and if it is not the case, the operation will throw a ClassCastException. A solution to this is to first check that the object is really of the desired type:

    Object object = array[i];
    if (object instanceof Employee) {
        Employee employee = (Employee) object;
        employee.getSalary();
    }
    else {
        System.err.println("Object is not an Employee: we can not call getSalary()!");
    }
    

    But you see that it becomes much more verbose and if you multiply this by the number of times you will have to call a method of these objects, then the code becomes unmanageable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I have array of objects, when I type the name of variable in
I have an array of objects and I want to add another object just
I have an array of objects[] but I want to convert them to an
I have an array of objects, and i use following code to get in
I have an array of objects . Each object has an attribute we'll call
Im stuck here, any hint would be nice. I have an array of Objects
How do I use another controllers methods inside of another controllers views? I have
I have an array with 32 objects inside. I am currently trying to figure
I have an array of Objects, each containing a Location and a Links array
I have an array of objects that have QTTime structs as attributes. The objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.