LordValkyrie
LordValkyrie

Reputation: 75

Can't access an object's methods from an ArrayList in Java

Java tells me it can't find getName() when i try to run the main method. I'm not sure why, I've looked around stackexchange to no avail. It seems like such a simple solution but I can't seem to figure it out.

import java.util.*;

public class ArrayTester
{
    public static ArrayList<Object> array;

    public static void main(String args[])
{
        array = new ArrayList<Object>();
        array.add(new TestObject("Some Name"));
        System.out.println("Object Name: " + array.get(0).getName());
    }
}

Object:

public class TestObject
{
    private String name;

    public TestObject(String cname){
        name = cname;
    }

    public String getName(){
        return name;
    }
}

Thanks for your help in advance. I apologize if there is an identical question somewhere that I didn't see.

Upvotes: 2

Views: 3846

Answers (4)

ChoockY
ChoockY

Reputation: 94

When you want to store different type of objects in the ArrayList, you should find out, what is common in these. For example all of these have a name. Then you can create an interface, called TestInterface:

public interface TestInterface {

     String getName();
}

The interface defines the getName() method and all the classes which implement it, contains the getName(), too.

The TestClass should implement the TestInterface:

public class TestObject implements TestInterface {

     private String name;

     public TestObject(String cname) {
         name = cname;
     }

     public String getName() {
         return name;
     }
 }

You can also write an OtherClass:

public class OtherClass implements TestInterface {

     public String getName() {
         return "OtherClass getName method";
     }
}

Your main:

public class ArrayTester {
     public static ArrayList<TestInterface> array;

     public static void main(String args[]) {
          array = new ArrayList<TestInterface>();
          array.add(new TestObject("Some Name"));
          array.add(new OtherClass());
          for (TestInterface t: array) {
               System.out.println("Object Name: " + t.getName());
          }
     }
}

You can pass instances of all classes which implement TestInterface to the ArrayList and when you iterate over it, Java call the concrete implementations of the getName() method.

The output:

Some Name
OtherCalss getName method

Cast can also work, but it isn't type safe. In the ArrayList of Object type you can pass all kinds of object. It is also OK, when you pass a String to it. It compiles, but when you try to cast the String to TestObject, you will have a ClassCastException.

Upvotes: 2

Tushar
Tushar

Reputation: 1470

You can use instanceof function and compare it in if block before appropriate casting :

public static void main(String args[])
{
    String name;
    array = new ArrayList<Object>();
    array.add(new TestObject("Some Name"));
    for (Object o : array) {
        if(o instanceof TestObject){
            System.out.println("Object Name: " + ((TestObject) o).getName());
        } 
    }
}

and of o is instance of some other class you can simple compare it in if block and call corresponding function after casting like :

public static void main(String args[])
{
    String name;
    array = new ArrayList<Object>();
    array.add(new TestObject("Some Name"));
    for (Object o : array) {
        if(o instanceof TestObject){
            System.out.println("Object Name: " + ((TestObject) o).getName());
        } 
        else if(o instanceof SomeObject){
            System.out.println("Object Name: " + ((SomeObject) o).getSomeObjectFunction());
        }
    }
}

I hope this will solve your problem.

Upvotes: 0

SatyaTNV
SatyaTNV

Reputation: 4135

Java tells me it can't find getName() when i try to run the main method

Cast Object class to TestObject class to use the methods of TestObject class. Since array.get(0) returns Object class as object. There is no such method means getName() exists in Object class.

change

System.out.println("Object Name: " + array.get(0).getName());

to

System.out.println("Object Name: " + ((TestObject)array.get(0)).getName());

Upvotes: 3

m.aibin
m.aibin

Reputation: 3603

You created a class named TestObject, so you should create array with TestObject, not Object:

public class ArrayTester
{
    public static ArrayList<TestObject> array;

    public static void main(String args[])
{
        array = new ArrayList<TestObject>();
        array.add(new TestObject("Some Name"));
        System.out.println("Object Name: " + array.get(0).getName());
    }
}

or at least cast it:

System.out.println("Object Name: " + ((TestObject) array.get(0).getName());

Upvotes: 1

Related Questions