2

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.

4 Answers 4

3

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());
Sign up to request clarification or add additional context in comments.

1 Comment

This worked, thanks a lot. I hadn't thought about needing to cast it this way.
2

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.

Comments

1

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());

1 Comment

While true, I wanted to create an arraylist that could accept different kinds of objects, not just TestObjects.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.