[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

Using my ArrayList from any method

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
jkteater
Asked:
jkteater
  • 3
1 Solution
 
for_yanCommented:
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
 
for_yanCommented:

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
 
wdosanjosCommented:
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
 
for_yanCommented:

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now