Results 1 to 8 of 8
Thread: Can not change variables.
- 09-08-2011, 09:11 PM #1
Can not change variables.
I have a problem.
Java Code:public abstract class AbstractsSimpleEncryption extends Object { public static char[] encryptCharS = {'-', '_', '*', '¯'}; //More code }Compile error when I try compile StringCrypting:Java Code:public class StringCrypting extends AbstractsSimpleEncryption { static { AbstractsSimpleEncryption.encryptChar = {'*', '@', '#', '§'}; } //More code }
Question, how can I change array encryptChar in class StringCrypting without getting compile error?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 = {'*', '@', '#', '§'};
- 09-08-2011, 10:47 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Can not change variables.
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.
- 09-08-2011, 11:31 PM #3
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 = {'-', '_', '*', '¯'}; }
- 09-09-2011, 01:47 AM #4
Re: Can not change variables.
I tried that, like this:
Java Code:public abstract class AbstractsSimpleEncryption extends Object { public static char[] encryptCharS = {'-', '_', '*', '¯'}; //More code }Even that give compile error o_0Java Code:public class StringCrypting extends AbstractsSimpleEncryption { public static char[] newShit = {'@', '*', '#', '&'}; AbstractsSimpleEncryption.encryptCharS = newShit; //More code }
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 01:52 AM.
- 09-09-2011, 02:07 AM #5
Re: Can not change variables.
It won't compile because that line of code is not inside a method or constructor (or initialiser block).
- 09-09-2011, 02:14 AM #6
Re: Can not change variables.
that fixed it, thanks
- 09-09-2011, 02:17 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.)
- 09-09-2011, 09:25 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
Similar Threads
-
Why does change one variable also change another?
By yma16 in forum New To JavaReplies: 1Last Post: 04-17-2011, 03:59 PM -
help with variables please
By arimaliz in forum New To JavaReplies: 3Last Post: 03-22-2011, 04:30 PM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
Variables
By mew in forum New To JavaReplies: 3Last Post: 12-11-2007, 12:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks