Results 1 to 7 of 7
- 01-20-2013, 03:02 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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;
- 01-20-2013, 06:37 PM #2
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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; }
- 01-21-2013, 02:35 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
Re: access array from another classes in java
from everything ive read today i guess this aint a good idea.
thanks all
- 01-21-2013, 10:30 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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
- 01-22-2013, 05:24 AM #5
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-22-2013, 06:00 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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
- 01-22-2013, 06:21 AM #7
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.
Similar Threads
-
How do I control access to data in a class from specific other classes?
By Skiller in forum New To JavaReplies: 4Last Post: 04-17-2011, 12:15 PM -
Making an array public or any variable to use across classes and Java files?
By Jonatan10 in forum New To JavaReplies: 12Last Post: 12-12-2010, 06:04 PM -
How to manage/access variables between classes
By dan0 in forum New To JavaReplies: 2Last Post: 04-03-2009, 12:53 AM -
How to access private data types from public classes?
By kevzspeare in forum New To JavaReplies: 3Last Post: 03-07-2009, 04:19 AM -
Object access between classes
By qazplm123890 in forum AWT / SwingReplies: 1Last Post: 03-05-2009, 05:00 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks