Results 1 to 3 of 3
  1. #1
    asterik123 is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Unhappy how to have function variables remember their values between calls

    Hi Everyone,
    I understand that we cannot declare static varibles in function definitions in java unlike in c++ that can retain their values between function calls. How can we do this in java, if the function should not accept parameters and should not reference the non-local variables.(this is a question for our assignment). Is there a way around it?can we declare static variables in c# lik in c++?

    thanks in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    if the function should not accept parameters and should not reference the non-local variables
    You are at a dead end with these two unless you want to use class variables, ie, variables with the static modifier. But these too are not local variables.
    The usual way that we preserve state is to use member variables.
    Java Code:
    class Pseudo {
        // Member variables with class scope.
        int x = 25;
        int y = 40;
        // Class variables.
        static int z = 99;
        static int v = 2;
    
        public void modifyVariables() {
            x += 25;
            y += 10;
            z -= 12;
            v += z;
            System.out.printf("x = %d  y = %d  z = %d  v = %d%n",
                               x, y, z, v);
        }
    }

  3. #3
    asterik123 is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default thanks

    Hi hardwired,
    thanks... I thought so too, without the 2 options i am pretty much at a dead end. now its confirmed!!!
    hansneet

Similar Threads

  1. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  2. Controlling method calls
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 01:14 PM
  3. JFileChooser remember the location
    By Mr tuition in forum AWT / Swing
    Replies: 3
    Last Post: 12-08-2007, 05:17 PM
  4. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:56 PM
  5. How to access system calls in java
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 03:12 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
  •