Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By quad64bit

Thread: access array from another classes in java

  1. #1
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default access array from another classes in java

    what is the best way to access the array from different classes?

    do i replace foo with the class that im in?

    do i replace arr with the array i want to get?

    thanks
    Java Code:
    ////////////////////////
    
    class Foo{
      private int arr[];
      public int[] getArr(){
        return this.arr;
      }
    }
    //from other class now
    
    new Foo().getArr();or else it needs to be static
    ///////////////////////////////////////////////
    class Foo{
      public static int arr[];
    }
    
    //from other class
    int arr[] = Foo.arr;

  2. #2
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: access array from another classes in java

    hey i put the first code in a class that will call the array and in the second code i put in the shuffler .
    but im getting an error java.lang.NullPointerException .


    am i doing it right ,i really need to access the arrays with everything in them.
    thanks



    Java Code:
                 int playershand[][] = Shuffler.playershand;
    	int computerhand[][] = Shuffler.computerhand;
    	int boneyard[][] = Shuffler.boneyard;



    Java Code:
                   static int playershand[][];
    	  public static int[][] getplayershand(){
    	    return playershand;
    	  }
    	  static int computerhand[][];
    	  public static int[][] getcomputerhand(){
    	    return computerhand;
    	  }
    	  static int boneyard[][];
    	  public static int[][] getboneyard(){
    	    return boneyard;
    	  }

  3. #3
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: access array from another classes in java

    from everything ive read today i guess this aint a good idea.
    thanks all

  4. #4
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: access array from another classes in java

    hey yall i still cant understand why passing an array is so bad, dont you pass single numbers all the time ,then some people call it a bad pratice,the program is designed to do things the fastest possible ,right?
    thanks all

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default Re: access array from another classes in java

    An array is a reference type. Once a method returns the array to the outside world, the calling method can change the value of elements of the array.

    It's better to return a clone of the array.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    flyhigh is offline Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0

    Default Re: access array from another classes in java

    you mean on the first one?
    so if i clone it only the clone can be messed up?
    thanks

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: access array from another classes in java

    Depends. Passing an array can be bad from an OOP perspective, or it could be totally fine. It depends on the context. If I want to search a collection for a name for instance, then it would be silly to retrieve the entire collection for the class that holds it and look myself - it would make more sense for the class that holds it to have a search or find method which I could simply pass parameters to and have it give me a result. That's all part of making your code modular and loosely coupled.
    DarrylBurke likes this.

Similar Threads

  1. Replies: 4
    Last Post: 04-17-2011, 12:15 PM
  2. Replies: 12
    Last Post: 12-12-2010, 06:04 PM
  3. How to manage/access variables between classes
    By dan0 in forum New To Java
    Replies: 2
    Last Post: 04-03-2009, 12:53 AM
  4. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM
  5. Object access between classes
    By qazplm123890 in forum AWT / Swing
    Replies: 1
    Last Post: 03-05-2009, 05:00 AM

Posting Permissions

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