lack of C's static local variables

J

John Davison

I was just wondering - coming from a mostly C background, Java lacks C's
static variables (one's local to a function but stay in memory.) Static
class members aside, what other techniques do people use when they want the
same kind of functionality?

John
 
D

Danny Woods

John Davison said:
I was just wondering - coming from a mostly C background, Java lacks C's
static variables (one's local to a function but stay in memory.) Static
class members aside, what other techniques do people use when they want the
same kind of functionality?

A local static variable is typically used in C when you need a function to
retain state between calls. Since Java has no freestanding functions, the
role of a local static is more or less taken over by a class variable: the
containing object has the state--individual methods don't. Unless special
circumstances require it, such variables don't normally need to be static
unless accessed from a static method.

There *is* a small loss in that any method associated with the class can
access the variable, whereas the function with a static local in C completely
hides this from the rest of the world. But then, Java has its own design
methodologies and views on what's good, which don't neccessarily agree with
C. This is not to say that either C or Java are 'wrong,' just different.

Regards,

Danny.
 
J

Jon A. Cruz

John said:
I was just wondering - coming from a mostly C background, Java lacks C's
static variables (one's local to a function but stay in memory.) Static
class members aside, what other techniques do people use when they want the
same kind of functionality?

I think the better question would be "Exactly what uses do you need this
functionality for?"


In other words *what* is it that you are doing when you use those; what
is your main intent? This is as opposed to *how* you achieve your goals.
In this case the *how* is to use a C static function variable. What is
the *why* ?
 
T

Tor Iver Wilhelmsen

John Davison said:
I was just wondering - coming from a mostly C background, Java lacks C's
static variables (one's local to a function but stay in memory.)

No, it's C that lacks Java's namespaces (read: classes). Method-static
variables are a hack around a lack of a proper namespace mechanism.
 
D

Digital Puer

Jon said:
I think the better question would be "Exactly what uses do you need this
functionality for?"


In other words *what* is it that you are doing when you use those; what
is your main intent? This is as opposed to *how* you achieve your goals.
In this case the *how* is to use a C static function variable. What is
the *why* ?


I nominate this reply for Most Worthless Posting of the Year.

Next time, try using your time in a more constructive manner
(like wiping your ass more thoroughly) rather than confusing the
original poster with your inane rhetorical questions.
 
C

Collin VanDyck

This applies to you as well.
Next time, try using *your* time in a more constructive manner.

This reminds me of this horrible recursive method I wrote one time.
 
T

Thomas Fritsch

Digital said:
I nominate this reply for Most Worthless Posting of the Year.

Next time, try using your time in a more constructive manner
(like wiping your ass more thoroughly) rather than confusing the
original poster with your inane rhetorical questions.
This applies to you as well.
Next time, try using *your* time in a more constructive manner.
 
G

George Elkins

John Davison said:
I was just wondering - coming from a mostly C background, Java lacks C's
static variables (one's local to a function but stay in memory.) Static
class members aside, what other techniques do people use when they want the
same kind of functionality?

John

An object's state can encapsulated (hidden) simply by using private data
members and public methods to change the object's state. It's much more
powerful and safe than C's local static variables.
 
J

John Davison

Jon A. Cruz said:
I think the better question would be "Exactly what uses do you need this
functionality for?"


In other words *what* is it that you are doing when you use those; what
is your main intent?

To keep data in memory between function calls without using global
variables.
This is as opposed to *how* you achieve your goals.
What?

In this case the *how* is to use a C static function variable. What is
the *why* ?

What a strange response.

I think Tor Wilhelmsen had the best answer for this question. Thanks to
all.

John
 
C

Carl Howells

John said:
To keep data in memory between function calls without using global
variables.




What?

Jon was actually quite clear, here. Think of it in terms of what you're
trying to accomplish, rather than how you want to accomplish it. That's
the basis of all good abstraction, and is a very important skill for
programmers to have.

Jon gave this response because you posed the question in terms of
mechanism, rather than in terms of goal. He wanted to know what your
goal was, in order to suggest an appropriate mechanism. The replacement
of a mechanism absent in Java but present in another language isn't a
process where everything has a fixed replacement. Rather, the
replacement usually depends, to some extent, on the goal the mechanism
was being used to achieve.
 
A

Adam P. Jenkins

John said:
I was just wondering - coming from a mostly C background, Java lacks C's
static variables (one's local to a function but stay in memory.) Static
class members aside, what other techniques do people use when they want the
same kind of functionality?

You just said the answer; private static class variables. When I need
to declare a static variable that is only going to be used by one
function, I've often thought it would be cleaner if I could declare it
inside that function like I can do in C/C++. To get a similar effect
in Java and at least make it clear that that static variable is just
meant to be used by that function, I declare the static just before the
function and prefix its name with the function name. For example:

class SomeClass {
// this variable is only meant to be used from
// formatDate()
private static DateFormat formatDate_format
= new SimpleDateFormat("format string");

public String formatDate(Date date) {
return formatDate_dateFormat(date);
}
}
 
D

Dale King

Digital Puer said:
I nominate this reply for Most Worthless Posting of the Year.

Next time, try using your time in a more constructive manner
(like wiping your ass more thoroughly) rather than confusing the
original poster with your inane rhetorical questions.

On the contrary, his reply is one of most on the point and most
constructive. We are constantly getting posts asking how to implement some
feature from another language, when the real answer is that there is no
direct correlation in Java. The right answer depends on what we are trying
to accomplish.

A Similar example is the recurring "How do I create an executable?" The
answer greatly depends on the goal. The answer may be something like GCJ,
create an executable jar, use java webstart, run it from a network drive,
package the JRE with the app, or use an installer. You cannot mechanically
answer the question without knowing they why of the question instead of the
what.

Another example I just answered recently someone was asking how to get a
reference to the application's directory. The question itself really has no
meaning in Java. The why was that he needed to read some data associated
with the app. He focused on how that was done in other languages. The real
answer is that the files should be packaged in the jar and accessed through
getResource, but without asking they why instead of the what you can't get
to that answer.
 
J

Jon A. Cruz

Digital said:
I nominate this reply for Most Worthless Posting of the Year.

Next time, try using your time in a more constructive manner
(like wiping your ass more thoroughly) rather than confusing the
original poster with your inane rhetorical questions.


Actually, that was not rhetorical in the least.

Generally over the years I've found again and again that people get too
caught up in the mechanics and asking about how to achieve the solution
they think they need to use to solve the problem, rather than asking
about the actual problem itself.


What is needed is stepping back. Looking at it from a higher level. etc.
 
T

Thomas Weidenfeller

Digital said:
I nominate this reply for Most Worthless Posting of the Year.

Next time, try using your time in a more constructive manner
(like wiping your ass more thoroughly) rather than confusing the
original poster with your inane rhetorical questions.

Whatever you smoke there at UCLA, it is not good for you.

/Thomas
 
C

Chris Uppal

Jon said:
Generally over the years I've found again and again that people get too
caught up in the mechanics and asking about how to achieve the solution
they think they need to use to solve the problem, rather than asking
about the actual problem itself.

It seems to me that is nearly always the case when someone is asking "how do I
do the equivalent of X from language Y?".

-- chris
 
R

Red Orchid

George Elkins said:
An object's state can encapsulated (hidden) simply by using private data
members and public methods to change the object's state. It's much more
powerful and safe than C's local static variables.

why "much more powerful and safe" ?

i do not think that it is much more powerful and safe than local
static variable.

the reasons is as follows:

first, let define the following terms.
1) 'pure' means 'do not have any local variable'.
2) 'stateful' means 'maintain a value during
applicatin run-time'.


in java,
methods of a class are not 'pure' ones.

Now, let consider about 'stateful'.

in programming,
i think that 'stateful' object(class) is important and
is used frequently and commonly.
therefore, a language have to give us simple/easy syntax
for 'stateful' implementation

local static variable VS private static data member.
this is a choice/problem of "how making 'stateful' ?".

in java, (as i know as)
local static variables of a method is not allowed.
therefore,
it will result in the following codes for static object:
===============================================
Public Class StateTest {
..
private static int mStateful = ..;
..

public StatefulMethod ( ... ) {
..
.. = mStateful .. ;
..
}
}
===============================================

but, in this codes,
'mStateful' is _not_ local variable of 'StatefulMethod'.
that is, other methods of 'StateTest' can access it, too.

it is not safe if it is required that only 'StatefulMethod'
have to access it.

moreover,
as the number of static data member increases,
the readability of source decreases and the confusion between
static data members increases.

i think that the following codes will match with
"encapsulated (hidden)" conception.
===================================================
Public Class StateTest {
..

public StatefulMethod ( ... ) {
static int mStateful = ..;
..
}
}
====================================================

and, as i know as,
almost IDEs support variable list.
 
J

John Davison

Carl Howells said:
Jon was actually quite clear, here. Think of it in terms of what you're
trying to accomplish, rather than how you want to accomplish it. That's
the basis of all good abstraction, and is a very important skill for
programmers to have.

Jon gave this response because you posed the question in terms of
mechanism, rather than in terms of goal. He wanted to know what your
goal was, in order to suggest an appropriate mechanism. The replacement
of a mechanism absent in Java but present in another language isn't a
process where everything has a fixed replacement. Rather, the
replacement usually depends, to some extent, on the goal the mechanism
was being used to achieve.

I see. I didn't have a goal in mind. I was just curious to see how other
Java programmers handled the situation.
 
J

Jon A. Cruz

Thomas said:
Whatever you smoke there at UCLA, it is not good for you.


Hmmm... UCLA...

Maybe I should drop a note to the professor there I used to work with.

;-)
 
J

Jon A. Cruz

John said:
I see. I didn't have a goal in mind. I was just curious to see how other
Java programmers handled the situation.

Ahhh... I see.

Then this gives enough context to give you a general answer.

In general, they do what's appropriate for the situation. :)




That is, I think 3 main cases come up

1) Replace it with the use of a static class variable.

2) Replace it with an instance variable.

3) Drop it's use altogether, as it probably was a massive kludge.



Remember, C had no objects at all, so if you needed to preserve state
for a function, you'd use a static variable in the function. However, in
Java you're not even allowed to have methods running around without a
class they belong to. So now that all methods have to at least belong to
a class, their 'static' data also logically belongs to that class or to
an instance of that class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top