How do i access a variable in an object which is in a arraylist

Posted on 2006-06-01
Last Modified: 2010-03-31

I am arraylist say results. This list holds students object. each student object has a firstname, lastname.

how do i access the value of firstname of student object inside the arraylist.

Question by:muthiahmerchant
    LVL 14

    Accepted Solution

    Object obj = arr.get(index);
    ((Student) obj).firstname;

    it could be more elegant to use Generic ArrayList --> eliminating the cast

    ArrayList<Student> arr = new ArrayList<Student>();
    LVL 14

    Expert Comment

    If You don't use generics, then You'll have to cast the type of Object to the one You had before putting it into ArrayList. Here how it is used:

    ArrayList al = new ArrayList();
    Integer int = new Integer(4);
    Object o = al.get(0); //this returns an object, without the real type of it
    Integer trueValue = (Integer)o; //so You must to cast the type back to what it was before

    another option is to use generics since java 1.5:

    ArrayList<Integer> al = new ArrayList<Integer>(); //only Integer objects will be allowed, including primitive type 'int'. This is possible because of autoboxing feature.
    Integer i = al.get(0); //and this works.

    to get more info on generics:
    LVL 10

    Expert Comment

    If your class variables are private then you can have getters and setters for the variables, How
    ever if they are not private you can acces them with the instance of the student. ex. stud.firstName , stud.lastName.

    import java.util.ArrayList;
    import java.util.ListIterator;

    public class Lis {
          public static void main(String[] args){
                ArrayList list = new ArrayList();
                list.add(new Student("jack","mukundh"));
                list.add(new Student("john","Abraham"));
                ListIterator iterator = list.listIterator();
                      Student stud = (Student);
                      System.out.println("First Name -->"+ stud.getFirstName());
                      System.out.println("Last Name -->"+ stud.getLastName());
    class Student
          private String firstName;
          private String lastName;
          public Student(String firstName,String lastName){
                this.firstName = firstName;
                this.lastName = lastName;
          public String getFirstName() {
                return firstName;
          public void setFirstName(String firstName) {
                this.firstName = firstName;
          public String getLastName() {
                return lastName;
          public void setLastName(String lastName) {
                this.lastName = lastName;
    LVL 1

    Author Comment

    solution provided by hoomanv although same as others was the first and solved my problem.

    Thanks to all

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now