java - Difficulty accessing an ArrayList in another class in the package -


my java experience limited , having problems accessing array list in class in separate file in same package.

it doesn't seem matter declare array ..it not accessible other classes

my top class like:

package 1st_class;  import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.arraylist;  public class 1st_class {       public void main(string[] args)      {         arraylist<test> tests = new arraylist<test>();          arraylist<score> scores = new arraylist<score>();          tests = new arraylist<test>();          scores = new arraylist<score>();          mainmenu menu1 = new mainmenu();         menu1.setvisible(true);     } } 

this seems recognise arrays in code ..i can reference having instance of mainmenu in other class , prefixing array name example main.

i confused scope guess.

try write getarraylist() method in class need or define static.

public class 1st_class {         public static arraylist<test> tests = new arraylist<test>();          public static arraylist<score> scores = new arraylist<score>();      public void main(string[] args)      {         mainmenu menu1 = new mainmenu();         menu1.setvisible(true);     } }  public newclass {      public newclass() {         1st_class .tests.add(new test);     }  } 

or can that:

 public class 1st_class {         private arraylist<test> tests = new arraylist<test>();          private arraylist<score> scores = new arraylist<score>();      public void main(string[] args)  {          mainmenu menu1 = new mainmenu();         menu1.setvisible(true);     }      public arraylist<test> getarraylist() {          return tests;     } }  public newclass {      public newclass() {         arraylist<test> temp = new 1st_class().getarraylist();     }  } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -