Results 1 to 8 of 8
Thread: Calling method problem
- 10-24-2010, 07:11 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Calling method problem
Hi below is an example of 2 classes that I run on BlueJ
test class
test1Java Code:import java.util.ArrayList; public class test { private ArrayList<String> a; public test() { a = new ArrayList<String>(); } public void addA(String atest) { a.add(atest); } public int sizeA() { return a.size(); } public void lista() { for(String atest : a) { System.out.println(atest); } } }
I can compile test class, create an object,call method "addA" and add some string and then call method "lista" (string in the ArrayList is displayed on the terminal window). But after doing so, when I compile test1 class, create an object, call method "list" which basically call method "lista" from test class; nothing happen. I would like when calling "list" method from test1 that it display the content of a ArrayList. Can anyone help me please???Java Code:public class test1 { private test a = new test(); public test1() { a.lista(); } public void list() { a.lista(); } }
- 10-24-2010, 07:14 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Please disregard the "a.lista();" in the test1 constructor
- 10-24-2010, 07:19 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
The class test1 (it should be named Test1) does not provide any method that will add strings to the private member a. Consequently a.lista() will always print exactly zero strings.
I would like when calling "list" method from test1 that it display the content of a ArrayList
It does display the contents of an array list. However as noted above that array list is empty.
- 10-24-2010, 08:04 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Ok but what if after adding some string in a ArrayList in class test by calling addA and now I would like to create a method in Test1 class that will display the added string in ArrayList. Is there a way that i can do this. Can you provide me with an example if possible.
- 10-24-2010, 08:16 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
OK, so you add the string using addA(). The problem is that addA() is a method of class Test. So what you have done is call the addA() method of some particular instance of Test.
There is simply no way that some other object entirely - an instance of Test1 - is going to be able to get at that string.
-----
Your Test1 class creates its own private instance of Test. (the thing called a). And this is where the problem starts.
Another approach would be to remove the private Test a initialiser in class Test1 and replace the constructor with one that took a Test argument. The Test1 constructor would use the argument to initialise the a variable.
Whatever is currently creating the Test1 instance would then also create the Test instance and pass a reference to it as an argument to the Test1 constructor. Now the addA() method could be called on the Test instance and the result would be visible when list() was called on the Test1 instrance.
- 10-24-2010, 08:22 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Java Code:import java.util.ArrayList; import java.util.List; public class Test { private List<String> a; public Test() { a = new ArrayList<String>(); } public void addA(String atest) { a.add(atest); } public void lista() { for(String atest : a) { System.out.println(atest); } } } public class Test1 { private Test a; public Test1(Test test) { a = test; } public void list() { a.lista(); } } public class Driver { public static void main(String[] args) { Test test = new Test(); Test1 test1 = new Test1(test); test.add("fee"); test.add("fie"); test.add("foe"); test.add("fum"); test1.list(); } }
(not actually tested)
- 10-24-2010, 08:33 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Thanks man. it works now. really appreciate your help.
- 10-24-2010, 09:06 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Similar Threads
-
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
Calling The main method from another method
By SwissR in forum New To JavaReplies: 3Last Post: 07-27-2010, 11:03 AM -
Problem calling a method
By ToeJam in forum New To JavaReplies: 5Last Post: 12-12-2009, 01:45 AM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks