[FR/EN][C++ to Java] How to match the C++'s behaviour when using static variable
Greetings !!
I want to use a C++ technique I used lot of times when calling a method from anywhere that consists to set a static variable that is initialised once but that the value never changed during the application life.
For example:
public int AClass::ThisC++Method(int aParameter)
{
static int ThisVariable=0;
...
Something happened that changed the value of ThisVariable
...
return Something
}
When you enter the second time in ThisC++Method the value of ThisVariable is the one it was just before the return statement was hit.
I want to use this feature in Java but it seems "static" in Java is used for different purposes, as the way to use a method of a class without declaring any instances of the class (just as ThisClass::ThisMethod() in C++, class method invocation vs instance method invocation if I'm right).
Code:
private void jFormattedTextFieldIPAddressKeyTyped(java.awt.event.KeyEvent evt) {
final int[] nextbyte={4,8,12};
final indiceOctet=0;
if(evt.getKeyChar()=='.')
{
((javax.swing.JTextField)evt.getSource()).setSelectionStart(nextbyte[indiceOctet]);
((javax.swing.JTextField)evt.getSource()).setSelectionEnd(nextbyte[indiceOctet]+3);
[INDENT]indiceOctet++;[/INDENT]
}
}
Here Java says "I can not modify the value referenced by indiceOctet..."
I need to have indiceOctet to keep the value once the method exits.
But I really don't know how I could do so...
I can not set static int the IDE says "illegal start of expression"...
Quote:
Static local variables: variables declared as static inside a function are statically allocated while having the same scope as automatic local variables. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again.
Is there a key word I don't know which is able to produce the same effect ??
Thank you for your patience ^^