Results 1 to 8 of 8
  1. #1
    jmoutia is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default Calling method problem

    Hi below is an example of 2 classes that I run on BlueJ

    test class
    Java 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);
            }
        }
    }
    test1
    Java Code:
    public class test1
    {
        private test a = new test();
        
        public test1()
        {
            a.lista();
        }
    
        public void list()
        {
            a.lista();
        }
    }
    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???

  2. #2
    jmoutia is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Please disregard the "a.lista();" in the test1 constructor

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  4. #4
    jmoutia is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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)

  7. #7
    jmoutia is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks man. it works now. really appreciate your help.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  3. Problem calling a method
    By ToeJam in forum New To Java
    Replies: 5
    Last Post: 12-12-2009, 01:45 AM
  4. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Posting Permissions

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