Results 1 to 6 of 6
  1. #1
    soundlord is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default [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).

    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]
            }
           
        }
    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"...
    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 ^^
    Last edited by soundlord; 03-17-2011 at 07:28 PM.

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

    Default

    I need to have indiceOctet to keep the value once the method exits.
    So make it a member variable.
    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.

  3. #3
    soundlord is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    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).


    Note: Trying to shoehorn C++ techniques into Java (this applies between any language frankly) almost always ends in tears.
    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...

    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 ^^

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by soundlord View Post
    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 ^^
    You can't cast a string to an int value in C++ either.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    soundlord is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    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++.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by soundlord View Post
    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++.
    Sure, operator overloading can do it, but operator overloading is an invention of the devil; it's like : "what you read is not what it is" ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 12-01-2010, 06:43 AM
  2. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  3. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  4. static variable
    By udhayageetha in forum AWT / Swing
    Replies: 17
    Last Post: 07-07-2008, 11:22 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 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
  •