Results 1 to 12 of 12
Thread: Variable not retaining value
- 06-10-2011, 03:40 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
Variable not retaining value
Hi,
So I delcared a couple arrayLists at the top of my program just under the public Class classname{. I assigned values to these arrayLists in a method called onEnable(). When I create a seperate method for each of these like:the elements of the array get reset to default values. How to i declare my varibles so they retain their values through methods?Java Code:public int[] getArray() { int[] arrayInt = new int[array.size()]; for (int i=0; i < arrayInt.length; i++) { arrayInt[i] = array.get(i).intValue(); } System.out.println("in array: " + arrayInt.length ); return arrayInt; }
-
It's hard to know where your problem lies based on the code snippet you've posted, but in general, for a variable to be visible and retain its value in multiple methods of the class, it needs to be declared in the class scope.
- 06-10-2011, 03:46 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
is this what you mean?
cause thats how i have it and its not retainingJava Code:public class Blockremover extends JavaPlugin { int numWorld = 0; ArrayList<Integer> array = new ArrayList<Integer>(); ArrayList<Integer> arraySplit = new ArrayList<Integer>(); ArrayList<String> worldArray = new ArrayList<String>();
- 06-10-2011, 03:48 AM #4
I too am not sure of what the problem is but one thing I want to mention is that the array and List do not store objects but rather references to objects. Therefore if you change the state of an object in the List then that change will also be seen by the array. Is this the problem you are seeing?
- 06-10-2011, 03:52 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
well im not sure. What im doing is reading in integers from a textfile and storing them in different ArrayLists, so would that be the problem then?
-
- 06-10-2011, 04:12 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
thats the reading in section to the array that is adjusted in the method i posted above. If you need other sections to be able to tell i'l post themJava Code:inputId = new FileReader("plugins/Blockremover/"+ worldArray.get(i)+".txt"); bufRead3 = new BufferedReader(inputId); for(int j=0; j<forTill ; j++){ array.add(Integer.parseInt(bufRead3.readLine())); } } bufRead3.close();
- 06-10-2011, 04:19 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
actually just declaring them as static made it work thanks for the help
-
- 06-10-2011, 04:24 AM #10
You just proved my point from this thread:
Error in Code: Non-static method cannot be referenced from a static context
- 06-10-2011, 09:49 PM #11
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
i read what you said in that post but can you explain the downsides to using static, im just curious
-
It can prevent your program's ability to be used in an object oriented fashion. Say if you want to use a collection of whatever object you're creating or to change the user interface and display this data in multiple windows, using static will mess this up as all instances of this object will share the same variable.
Again, there are likely better solutions for your problem, and if you can show us more about your problem and your code, then we can tell you more about the better solutions.
Similar Threads
-
Colour increasing and retaining animation
By monica in forum Java 2DReplies: 7Last Post: 05-15-2011, 04:56 PM -
Retaining values entered by users in the installer fields when they come back to that
By salman.kagzi in forum New To JavaReplies: 0Last Post: 11-30-2010, 05:09 PM -
Retaining value of checkbox when returning back from a servlet
By sashi799 in forum Java ServletReplies: 18Last Post: 07-19-2010, 12:18 PM -
Rotated Shape Object Line weight is not retaining properly in printing
By dorairaj in forum AWT / SwingReplies: 7Last Post: 10-06-2009, 05:58 AM -
Trouble retaining transparent back color while setting opacity
By playwin2 in forum Java 2DReplies: 4Last Post: 03-26-2009, 09:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks