Results 1 to 8 of 8
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Can not change variables.

    I have a problem.


    Java Code:
    public abstract class AbstractsSimpleEncryption extends Object
    {
    	public static char[] encryptCharS = {'-', '_', '*', '¯'};
    
    	//More code
    }
    Java Code:
    public class StringCrypting extends AbstractsSimpleEncryption
    {
    	static
    	{
    		AbstractsSimpleEncryption.encryptChar = {'*', '@', '#', '§'};
    	}
    	
    	//More code
    }
    Compile error when I try compile StringCrypting:
    Java Code:
    C:\Program\Java\jdk1.7.0\jre\classes\pjjava\crypt\StringCrypting.java:7: error: illegal start of expression
    		AbstractsSimpleEncryption.encryptChar = {'*', '@', '#', '§'};
    		                                        ^
    C:\Program\Java\jdk1.7.0\jre\classes\pjjava\crypt\StringCrypting.java:7: error: not a statement
    		AbstractsSimpleEncryption.encryptChar = {'*', '@', '#', '§'};
    		                                         ^
    C:\Program\Java\jdk1.7.0\jre\classes\pjjava\crypt\StringCrypting.java:7: error: ';' expected
    		AbstractsSimpleEncryption.encryptChar = {'*', '@', '#', '§'};
    Question, how can I change array encryptChar in class StringCrypting without getting compile error?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Can not change variables.

    Quote Originally Posted by Pojahn_M View Post
    how can I change array encryptChar in class StringCrypting
    You can change its contents by assigning chars to the various array positions. You can change the array referenced by the value of AbstractSimpleEncryption.encryptChar by assigning a new array to that variable.

    What you can't do is just put some chars within {} and hope that one or other of the above will happen. Thinking that you can is a very common error because of the "shortcut" syntax you can use to create and initialise an array such as you use in the AbstractSimpleEncryption class: this is only available when a variable is being declared.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can not change variables.

    Would this work:
    Java Code:
    HoldCharArray.someChars = [COLOR="#FF8C00"]new  char[] [/COLOR]{'a', 'b'};  
    
    
     static class HoldCharArray {
    	public static char[] someChars = {'-', '_', '*', '¯'};
     }

  4. #4
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Can not change variables.

    Quote Originally Posted by pbrockway2 View Post
    You can change the array referenced by the value of AbstractSimpleEncryption.encryptChar by assigning a new array to that variable.
    I tried that, like this:

    Java Code:
    public abstract class AbstractsSimpleEncryption extends Object
    {
    	public static char[] encryptCharS = {'-', '_', '*', '¯'};
    
    	//More code
    }
    Java Code:
    public class StringCrypting extends AbstractsSimpleEncryption
    {
    	public static char[] newShit = {'@', '*', '#', '&'};
    	AbstractsSimpleEncryption.encryptCharS = newShit;
    
    	//More code
    }
    Even that give compile error o_0

    Java Code:
    D:\˜”°º•„  A N N A T „•º°”˜¨\Java\Pojahns.java:38: error: <identifier> expected
    	AbstractsSimpleEncryption.encryptCharS = newShit;
    				 	      ^
    1 error
    
    Tool completed with exit code 1
    Last edited by Pojahn_M; 09-09-2011 at 02:52 AM.

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

    Default Re: Can not change variables.

    It won't compile because that line of code is not inside a method or constructor (or initialiser block).

  6. #6
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Can not change variables.

    that fixed it, thanks

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Can not change variables.

    You say that you tried to assign a new array to the variable - but I don't see the "new" keyword. If you aren't declaring a variable with the shortcut syntax then assigning a new anything has got to include that keyword.

    (Sorry for being a b@stard and not posting teh code, but is an important brick wall to bang your head against for a while.)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Can not change variables.

    Can I just say that, the immediate problem aside, this code looks rather concerning?
    The idea of changing a static variable like this simply because of some class that's been accessed seems like it's a really awkward bug waiting to happen.

Similar Threads

  1. Replies: 1
    Last Post: 04-17-2011, 04:59 PM
  2. help with variables please
    By arimaliz in forum New To Java
    Replies: 3
    Last Post: 03-22-2011, 05:30 PM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 PM
  4. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 01:44 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
  •