Results 1 to 10 of 10
  1. #1
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Using an item in another class which is created in another class.

    I have a class which creates an array, ill call this class1. In another class (class2) I want to use this array, i have created the class1 object within class2 but I want to loop through the array from class1 in class2. How would i do this? This is what i tried so far.

    public void play()
    {
    POC.createFullDeck();
    POC.shuffle();
    for(Card c : POC){
    ..
    }
    }

    Thanks for any help.

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

    Default Re: Using an item in another class which is created in another class.

    Class2 should create a Class1 object and place it in a Class1 variable, and then it should call public methods on it in order to change its state or to ascertain its state.

  3. #3
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Using an item in another class which is created in another class.

    Do you mean like this? I get an incompatible type error when i try to compile.

    ArrayList<Card> playDeck = POC.createFullDeck();
    POC.shuffle();
    for(Card c : playDeck){

    }

    Thanks.

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

    Default Re: Using an item in another class which is created in another class.

    I don't really have an idea of what you're trying to do with this small unformatted code block. Consider telling us more and showing us more, including some well-formatted code that is posted here using tags so that it retains its formatting. Consider posting any and all error messages and indicating with a comment in the code the line(s) that cause the errors.

  5. #5
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Using an item in another class which is created in another class.

    I have to make a card game. In a class called PileOfCards, an array is created of type Card and type Card has two parameters; a suit and a value. In the play class, I need to use the arraylist which is created in the PileOfCards class. I don't know how to use this method whilst keeping the method public. I don't want to post any of the code given to me by the lecturer and i understand that that will make it harder to help me, so if you can't without it, it doesnt matter.

    Thanks for your help.

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

    Default Re: Using an item in another class which is created in another class.

    PileOfCards should not expose its fields to any other class, including Play, but instead should expose public methods. If you want PileOfCards to deal a Card, then give it a deal() method that removes a Card from the Card array and returns the removed Card. Then Play can create a PileOfCards object and call its deal method like so:

    Java Code:
    PileOfCards pileOfCards = new PileOfCards();
    
    Card someCard = pileOfCards.deal();  // we call this on the variable, not the class
    Make sense?

  7. #7
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Using an item in another class which is created in another class.

    Thank you so much! My code now works!

    So should I always return individual objects from an ArrayList to the class I want to use them in rather than trying to return the whole ArrayList?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Using an item in another class which is created in another class.

    Generally, yes.
    The reason is that if you return the array itself then it can be altered by code other than the code that "owns" the array. So cards could be added or removed from it, which shouldn't be allowed.
    Please do not ask for code as refusal often offends.

  9. #9
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Using an item in another class which is created in another class.

    Okay, so is that why my IDE kept giving me a warning message?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Using an item in another class which is created in another class.

    Depends how you set it up.
    And what the message is.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Amount of .class files created
    By Lowest0ne in forum Advanced Java
    Replies: 4
    Last Post: 12-03-2012, 04:02 PM
  2. Access object created in another class
    By BalintD in forum New To Java
    Replies: 5
    Last Post: 01-04-2012, 11:06 AM
  3. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  5. create a tree when a new class is created
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:58 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
  •