Results 1 to 12 of 12
  1. #1
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default 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:
    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;
    	}
    the elements of the array get reset to default values. How to i declare my varibles so they retain their values through methods?

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

    Default

    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.

  3. #3
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    is this what you mean?
    Java 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>();
    cause thats how i have it and its not retaining

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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?

  5. #5
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    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?

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

    Default

    Quote Originally Posted by MichaelT View Post
    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?
    It depends on how you do it, and again, based on the snippets you've posted, it's hard to say. You really need to give more information if you need help. We are terrible at reading minds and at guessing at missing code.

  7. #7
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Java 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();
    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 them

  8. #8
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    actually just declaring them as static made it work thanks for the help

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

    Default

    Quote Originally Posted by MichaelT View Post
    actually just declaring them as static made it work thanks for the help
    No -- That is the wrong solution and will bite you. You still need to post more code for us to give you the correct solution.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

  11. #11
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    i read what you said in that post but can you explain the downsides to using static, im just curious

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

    Default

    Quote Originally Posted by MichaelT View Post
    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

  1. Colour increasing and retaining animation
    By monica in forum Java 2D
    Replies: 7
    Last Post: 05-15-2011, 04:56 PM
  2. Replies: 0
    Last Post: 11-30-2010, 05:09 PM
  3. Replies: 18
    Last Post: 07-19-2010, 12:18 PM
  4. Replies: 7
    Last Post: 10-06-2009, 05:58 AM
  5. Replies: 4
    Last Post: 03-26-2009, 09:42 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
  •