Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By quad64bit

Thread: How to access an ArrayList in another class?

  1. #1
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default How to access an ArrayList in another class?

    Hi,

    I've been suffering another problem. I tried moving an ArrayList from a class (1) to class (2). Now I want to have still the full access to it but somehow Eclipse is just showing me an error message. It's just not working, and I can't imagine a reason why...

    class (1):
    import java.util.*;

    public class class1{
    public static void main(String args[]){

    class2 Object = new class2();
    Object.class2();

    System.out.println(word);
    }
    }
    class (2):
    import java.util.*;

    public class class2 {
    public void class2(){
    ArrayList<String> word = new ArrayList<String>();

    word.add("uno");
    word.add("dos");
    word.add("tres");
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: How to access an ArrayList in another class?

    What exactly is the error message? We can probably guess, but why make us?

    The scope of word is inside class2 (which should be Class2, follow standard naming conventions), so to get at it from another class, you have to pass it through a getter or setter method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to access an ArrayList in another class?

    The error message is:

    "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    word cannot be resolved to a variable

    at class1.main(class1.java:8)"

    I just thought it would work since a simple System.out.println(); can be passed from a class to another by just writing something like this: "classx Object = new classx();
    Object.classx();"

  4. #4
    nithinv is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: How to access an ArrayList in another class?

    if you want to access an ArrayList in another class create a method which will have return type of ArrayList as shown below:

    class (1):

    import java.util.*;

    public class class1{
    public static void main(String args[]){

    class2 Object = new class2();
    ArrayList word = Object.method2();

    System.out.println(word);
    }
    }
    class (2):

    import java.util.*;

    public class class2 {
    public ArrayList method2(){
    ArrayList<String> word = new ArrayList<String>();

    word.add("uno");
    word.add("dos");
    word.add("tres");

    return word;
    }
    }

  5. #5
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to access an ArrayList in another class?

    Quote Originally Posted by KevinWorkman View Post
    The scope of word is inside class2 (which should be Class2, follow standard naming conventions), so to get at it from another class, you have to pass it through a getter or setter method.
    Sorry, but since I'm new to Java, I don't get things that fast and I'm not able to realize it (or I rather don't know how to combine the getter with an ArrayList. I just know how to do it with variables). Can you explain it to me, please?

    EDIT: When trying to create a getter-setter function there's an error message saying "The operation is not applicable for the current selection. Select a field which is not declared as type variable or type that declares such fields."

    EDIT2:
    Quote Originally Posted by nithinv View Post
    if you want to access an ArrayList in another class create a method which will have return type of ArrayList as shown below
    But why do I have to do it this way?
    Last edited by Alpa; 12-29-2011 at 08:45 PM.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to access an ArrayList in another class?

    "But why do I have to do it this way?"

    You certainly do not have to but it is generally bad programming practice not to. Getters and Setters are common methods for preserving restricted access to data and modularity. You can make an app work without them, but it involves making everything public, which is bad practice. An example getter setter would be something like this:

    Java Code:
    private ArrayList myList = new ArrayList();
    
    public void setMyList(ArrayList myList){
        this.myList = myList;
    }
    
    public ArrayList getMyList(){
        return myList;
    }

  7. #7
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to access an ArrayList in another class?

    But why is it bad practice to make everything public? Actually it doesn't harm my code, does it?

    [JAVA]
    private ArrayList myList = new ArrayList();

    public void setMyList(ArrayList myList){
    this.myList = myList;
    }

    public ArrayList getMyList(){
    return myList;
    }
    [/JAVA]

    The ArrayList which I marked using bold, is it just a name that I can give my ArrayList? Could I just simply name it to something like "Hello" or "Cheese"? Or is it the classes name in this case?

    And couldn't I just leave out the setters part? I understand the function of the getter, just making it for my main method possible to have access to the wanted methodby "getting" it. But the setter's function isn't clear to me.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to access an ArrayList in another class?

    But why is it bad practice to make everything public?
    This is a bad example, but I hope it gets the idea across. Lets just say for argument's sake (you would never do this literally, but a similar situation can be just as bad) that you had a list of social security numbers hard coded into your program. They're needed for whatever reason. You have them in a public array. Now, because it is public, someone else using your software, even if they didn't have the code, could access this information from their own program simply by subclassing the your class with the public information, or calling it's data directly. Since all data is public, it is all right there.

    Your program will run with public data, but it allows this data to be accessed by anyone that cares. From a design perspective it can also be problematic, as subclasses and related classes can see data they really shouldn't, which can lead to situations where to 'owner' of the data isn't clearly defined, or way down the road with multi-threading, you can have concurrency problems.

    The ArrayList which I marked using bold, is it just a name that I can give my ArrayList?
    The bold word is the data type, in this case, "ArrayList". The name of this variable is 'myList'. You can substitute any name you like in place of 'myList'.

    Could I just simply name it to something like "Hello" or "Cheese"?
    Yes

    Or is it the classes name in this case?
    ArrayList is the class name, that cannot change, myList is the variable name, that can be anything you like

    And couldn't I just leave out the setters part?
    Sure! If you will never need to 'set' the array list to something else, you don't need it. I just gave you an example of both the getter and the setter, as these are ubiquitous programming patterns which you will find useful down the road.

    just making it for my main method possible to have access to the wanted methodby "getting" it.
    Mostly... It isn't for the main method actually, it is for any class/code that is out of scope. If you are within the same class file and/or within the same code block, you have access to it even without the getter/setter. But if you are inside another class, then you don't unless it is public. The getters/setters allow you to specify what other code has access to. If you do not provide a setter, then other code cannot 'set' the value of the variable, which can be a good thing, as it gives you control over who and what can access data.

    But the setter's function isn't clear to me.
    Here is an example:

    Java Code:
    public class A{
       private int someNumber = 7;
       
       public void setSomeNumber(int number){
          someNumber = number;
       }
       
       public int getSomeNumber(){
          return someNumber;
       }
       
    }
    
    public class B{
       //an instance of A
       private A myA = new A();
       
       //main method
       public static void main(String[] args){
          new B();
       }
       
       //Constructor for B
       public B(){
          System.out.println(myA.getSomeNumber()); //will print 7
          myA.someNumber = 14; //Illegal, because someNumber in A is private!
          //so, we use the setter to do the same thing:
          myA.setSomeNumber(14);
          System.out.println(myA.getSomeNumber()); //will print 14
       }
    }
    Edit: The above code sample is supposed to be two separate .java files, I didn't make that clear. You would have A.java and B.java. You would attempt to run the program from B.java
    Last edited by quad64bit; 12-30-2011 at 04:18 PM.
    Alpa likes this.

  9. #9
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to access an ArrayList in another class?

    Thanks a lot for helping me :). I think I got the important facts now.

    But if I don't store any sensible information there shouldn't be a problem when setting everything to public, right? I mean I don't really care whether someone decompiles my code in order to see twisted noob scripts :D.

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to access an ArrayList in another class?

    But if I don't store any sensible information there shouldn't be a problem when setting everything to public, right?
    Correct, it is just bad practice. It is a good habit to get into now to do it the right way, but for hacking, you're right, making it public will not cause it not to function :)

Similar Threads

  1. Replies: 1
    Last Post: 11-04-2011, 07:14 AM
  2. Get an ArrayList from another class
    By ScienceLife in forum New To Java
    Replies: 5
    Last Post: 04-09-2011, 10:13 AM
  3. Replies: 0
    Last Post: 04-04-2010, 10:06 AM
  4. How can i store ArrayList objects in Access database
    By frankycool in forum Advanced Java
    Replies: 3
    Last Post: 11-04-2009, 06:55 AM
  5. Access LinkedList from another class
    By jboy in forum New To Java
    Replies: 20
    Last Post: 09-12-2009, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •