Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

static variables inside functions

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I don't know how to do in Java "static" variables inside functions(methods) like in C++.
I want to retain a value between function calls and I don't want (I can't) use global variable(object member data).
private void myfunction(){
int ione = 10;
//How initialize "ione" only once ???
//static int ione = 10 <--- error
...
...
}
Please help me on this.
Thanks.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dajac,
As you just found out, method local variables cannot be declared static. If you want a persistant (static) variable, you'll have to declare it at the class level something like this:

Hope this helps
Michael Morris
SCJP2
 
dar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Michael.
Thank You for your answer.
I don't want use object-member-data(= class level something).
But if method local variables cannot be declared static, then I must use class level variable.
bye.

Originally posted by Michael Morris:
Hi Dajac,
As you just found out, method local variables cannot be declared static. If you want a persistant (static) variable, you'll have to declare it at the class level something like this:

Hope this helps
Michael Morris
SCJP2

 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dajac,
You only have 3 choices.
1) Either the variable is declared IN a method in which case it is local to the method and therefore dies when the method ends.
Or 2) - the variable is "object member data" which means that each and every instance of the class has a field to track this value, and it can be used inside methods, but it's value depends on WHICH instance of the class that you are refering to.
Or - 3) the variable is static, which means that the variable is kept in the central classfile and all of the instances can access the value equally and the value of the variable at any given time is the same for all instances of the class.
What is it that you are trying to accomplish that can not be accomplished with one of these scenarios?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't know how to do in Java "static" variables inside functions(methods) like in C++.
I want to retain a value between function calls and I don't want (I can't) use global variable(object member data).


The static modifier for local variables in C++ has no counterpart in Java. If you really must avoid using class level variables you could try recursing the method and pass a local copy as you go:
 
dar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interest idea.
Thank you. I'l try it.
bye.
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic