Results 1 to 6 of 6
- 03-17-2011, 06:47 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
[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).
Here Java says "I can not modify the value referenced by indiceOctet..."Java 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] } }
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"...
Is there a key word I don't know which is able to produce the same effect ??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.
Thank you for your patience ^^Last edited by soundlord; 03-17-2011 at 07:28 PM.
- 03-18-2011, 09:42 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
So make it a member variable.I need to have indiceOctet to keep the value once the method exits.
If you want to maintain a value between instances then make it a static (class) variable.
Note: Trying to shoehorn C++ techniques into Java (this applies between any language frankly) almost always ends in tears.
- 03-18-2011, 06:30 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
Yep finally is what I did... I just wanted to be sure there was no other way (or a global variable modified in a local method but that sounds bad).
Yes, when discovering a new world, the bad habit consists in trying to seek for our landmarks instead of trying to open wider our minds and seek for new paths...Note: Trying to shoehorn C++ techniques into Java (this applies between any language frankly) almost always ends in tears.
I got the same situation with the casting of a String into int... I took a lot of time before try to use the static method from Integer class to convert it... I tried my usual C++ casting stuff to try the conversion withou success of course ^^
Thx and see you in the next episode ^^
- 03-18-2011, 08:21 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 03-18-2011, 08:57 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
Yes but the override of operator= permits to go further... I wrote "cast" but there is lot of manners to workaround ;)
You're right anyway, there is no way to cast from String to int in C++.
- 03-19-2011, 07:27 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
non-static variable cannot be referenced from static context...
By MadJack in forum New To JavaReplies: 5Last Post: 12-01-2010, 06:43 AM -
non-static variable cannot be referenced from a static context
By keo in forum New To JavaReplies: 5Last Post: 10-15-2010, 04:21 AM -
non-static variable grade cannot be referenced from a static context
By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:59 AM -
static variable
By udhayageetha in forum AWT / SwingReplies: 17Last Post: 07-07-2008, 11:22 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks