Results 1 to 10 of 10
- 12-28-2011, 03:43 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
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):
class (2):import java.util.*;
public class class1{
public static void main(String args[]){
class2 Object = new class2();
Object.class2();
System.out.println(word);
}
}
import java.util.*;
public class class2 {
public void class2(){
ArrayList<String> word = new ArrayList<String>();
word.add("uno");
word.add("dos");
word.add("tres");
}
}
- 12-28-2011, 03:45 PM #2
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!
- 12-29-2011, 10:29 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
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();"
- 12-29-2011, 11:05 AM #4
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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;
}
}
- 12-29-2011, 08:38 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
Re: How to access an ArrayList in another class?
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: But why do I have to do it this way?Last edited by Alpa; 12-29-2011 at 08:45 PM.
- 12-29-2011, 09:17 PM #6
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; }
- 12-30-2011, 11:35 AM #7
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
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.
- 12-30-2011, 04:14 PM #8
Re: How to access an ArrayList in another class?
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.But why is it bad practice to make everything public?
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 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'.The ArrayList which I marked using bold, is it just a name that I can give my ArrayList?
YesCould I just simply name it to something like "Hello" or "Cheese"?
ArrayList is the class name, that cannot change, myList is the variable name, that can be anything you likeOr is it the classes name in this case?
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.And couldn't I just leave out the setters part?
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.just making it for my main method possible to have access to the wanted methodby "getting" it.
Here is an example:But the setter's function isn't clear to me.
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.javaJava 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 } }Last edited by quad64bit; 12-30-2011 at 04:18 PM.
- 12-30-2011, 11:18 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
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.
- 12-30-2011, 11:30 PM #10
Re: How to access an ArrayList in another class?
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 :)But if I don't store any sensible information there shouldn't be a problem when setting everything to public, right?
Similar Threads
-
What does : Access to any element of an ArrayList is O(1). mean?
By kyle_maddisson in forum New To JavaReplies: 1Last Post: 11-04-2011, 07:14 AM -
Get an ArrayList from another class
By ScienceLife in forum New To JavaReplies: 5Last Post: 04-09-2011, 10:13 AM -
Problem to access data when a class calls another class
By ea09530 in forum New To JavaReplies: 0Last Post: 04-04-2010, 10:06 AM -
How can i store ArrayList objects in Access database
By frankycool in forum Advanced JavaReplies: 3Last Post: 11-04-2009, 06:55 AM -
Access LinkedList from another class
By jboy in forum New To JavaReplies: 20Last Post: 09-12-2009, 08:16 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks