Solved

Using my ArrayList from any method

Posted on 2011-09-19
4
320 Views
Last Modified: 2012-05-12
I want to be able to access my ArrayList in any method in my class without having to pass it to the method.  Example would be

public int getRowCount() {
                  int rows = aList.size();
                   return rows;
               } // end getRowSize()

Where Alist is my ArrayList

I am putting ArrayList in the Constructor, but that don't seem to do it.

public SelectedTModel() {
      super();
     ArrayList<RevDataset> aList = EdiSelection.rds;
}
0
Comment
Question by:jkteater
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36562661
you need to declare it as an intancwe varuiable
and assign in the constructor:
public class MyClass{
ArrayList  a;

MyClass(ArrayList a){

this.a = a;


}


...


Then it will be accessible everuywhere
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36562672

when you do this in the constructior
  ArrayList<RevDataset> aList = EdiSelection.rds;
you aList becvomes a local; varaiable
of the constructorr

You need to assign arrauylit to sthe instance
varaible of your class, then it would be accesible anyware in the class
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 50 total points
ID: 36562673
Create your ArrayList as a class variable something like this:

public class YourClass {
    public int getRowCount() {
        int rows = this.aList.size();
        return rows;
    } // end getRowSize()

    private ArrayList<RevDataset> aList;

    public SelectedTModel() {
        super();
        this.aList = EdiSelection.rds;
    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36562681

when you do this in the constructior
  ArrayList<RevDataset> aList = EdiSelection.rds;

you declaration
 ArrayList<RevDataset> aList
hapopnes inside constructor - so it is local to the constructor

decalartion should be outside any method then it is an instance variable
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tomcat: Unable to run tomcat service. 2 33
jsp login check 12 61
What is the use of Forwarding Class in java 1 51
How to fix  socket closed error 11 60
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

736 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