Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default Same Behavior of VB's Method's Static Variable

    Sorry I Need to revise this for wrong example:

    VB: static variable of method

    CallingMethod()
    Debug.Print( Method1( 1 ) )
    Debug.Print( Method1( 2 ) )
    End Sub

    Method1( intValue As Integer ) As String
    Static Var1 As Intger
    Var1 = Var1 + intValue
    Return Var1
    End Sub

    Output:
    3

    Note: variable of a method declared by Static...you can set the variable several times but the previous value still remains. And continue its value during the second call.

    Please help me to implement this on Java Servlet.

    e.g. this is only example please give me other snippets that allows the functionality of VB's Method's Static variable
    class Class1 {
    String Method1 (String strValue)
    {
    static String Var1 = null;
    Var1 = strValue;
    return Var1;
    }
    }

    Eros
    Tokyo, Japan
    Last edited by erosszz_jp@yahoo.co.jp; 10-30-2008 at 09:57 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    You cannot use the static modifier inside a method. Static should be part of the class and not part of the object instance.

    For your problem I think you can make your method parameter as final, so the value cannot be change as it is final.

    Java Code:
    public String method1(final String value) {
      // Do something here
      return value;
    }

  3. #3
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    For your problem I think you can make your method parameter as final
    Nope. The finality will only last as long as one invocation of the method

  4. #4
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default Thanks but....

    Yes, you're both correct but... still the question was not answered.

    I know Static cannot be used inside the Method in Java that's why I added the "e.g.". Simply means that one is for example only.

    Developer per ce, we think by the functionality not the syntax/semantics.

    The Final keyword of Java is simply like the Constant variable that can't be change the value anymore.

    For not familiar in VB, Static keyword inside the Method is likely as a constant variable but you can set the value once then after that it store the previous value for second use.
    And one more thing, a variable declared by Constant variable might be an error when you try to set it's value but the Static variable may set the variable once at first then you may also set the variable but still remains its last value after the method then continue from its last value during the second call.

    e.g.
    Constant var1 = 2

    if you do this it becomes an error:
    var1 = 3

    e.g. declared inside the method
    Static var1 = 0

    if you do this it becomes valid but still the first value:

    var1 = var1 + 3
    exit from method

    var1 = var1 + 5
    exit from method

    var1 = var1 + 1
    Output var1
    exit from method

    Output will be 9



    Please help me to implement this functionality in Java.

    Thanks in advance.

    Yo ro shi ku O ne gai shi ma su.


    Eros
    Tokyo, Japan
    Last edited by erosszz_jp@yahoo.co.jp; 10-30-2008 at 09:53 AM.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    zoomix is offline Member
    Join Date
    Oct 2008
    Location
    Sweden, Gothenburg
    Posts
    21
    Rep Power
    0

    Default

    Java's keyword "static" and VB's keyword "static" mean two different things, as you know. This snippet of code should do what you need.

    Java Code:
    public class Class1 {
     private int remeberedValue = 0;
    
     public int method1(int suppliedValue) {
      remeberedValue = remeberedValue + suppliedValue;
      System.out.println("rememberedValue: " + rememberedValue);
      return remeberedValue;
     }
    }
    C# is easily translatable into Java code. VB is not. You will need to read up on it.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by zoomix View Post
    Java's keyword "static" and VB's keyword "static" mean two different things
    I think mean the same thing. Usage or the scope is the difference, isn't it?

Similar Threads

  1. Static variable context Errors ?
    By Shyam Singh in forum New To Java
    Replies: 16
    Last Post: 08-08-2008, 09:11 PM
  2. static variable
    By udhayageetha in forum AWT / Swing
    Replies: 17
    Last Post: 07-07-2008, 11:22 AM
  3. About static variable
    By MarkWilson in forum New To Java
    Replies: 5
    Last Post: 06-27-2008, 01:43 PM
  4. Replies: 1
    Last Post: 08-01-2007, 09:25 PM
  5. Help with static variable counter
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-19-2007, 07:53 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
  •