Results 1 to 8 of 8
  1. #1
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default help with passing objects between classes

    hi,I'm new to java and have this problem.

    i have two clsses A and B

    public class A{
    ArrayList<Food> food=new ArrayList<Food>();

    B b1=new B();

    void display(){
    ---------
    ----------
    }
    }


    public class B{

    -----------------
    -----------------

    }

    now i want to access arraylist in class A from class B object (b1)

    is it possible without using static keyword?

    i want to send objects to array list in class A while staying in side clss B type object b1.

    hope you understand what I'm saying
    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    why not simply pass the array either as a parameter to B's constructor or as a method's parameter.

  3. #3
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    why not simply pass the array either as a parameter to B's constructor or as a method's parameter.
    well i tried it as a constructor.but it somehow didnnt worked properly.(its a gui project and when i pass it through constructor dialog box get messed)

    can you show me how to do it using a method?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Java Code:
    private ArrayList<Food> foodList;
    
    // constructors, etc...
    
    public void setFoodList(ArrayList<Food> foodList)
    {
      this.foodList = foodList;
    }
    It's pretty much the same as any setter method. What were you messing up on?

  5. #5
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Java Code:
    private ArrayList<Food> foodList;
    
    // constructors, etc...
    
    public void setFoodList(ArrayList<Food> foodList)
    {
      this.foodList = foodList;
    }
    It's pretty much the same as any setter method. What were you messing up on?
    but point is if we add this method to class A and then create class B object ,i cant access this setFoodList method from class B object.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I never said anything about adding this method to A. A has the ArrayList and tt's B that needs the list. So which class should have a setter method?

  7. #7
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    setter is in the A class and i call it from class B?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Passing values between classes problem.
    By alin_ms in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 06:49 PM
  2. Need help passing data between classes
    By bri1547 in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 04:19 AM
  3. Passing objects in Java
    By jbostjr in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 10:15 PM
  4. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 08:09 PM
  5. Passing objects in Java
    By jbostjr in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2007, 05:57 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
  •