Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #26249
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2013-10-09 07:49 -0700 |
| References | <slrnl5ape4.mol.avl@gamma.logic.tuwien.ac.at> |
| Message-ID | <4ca67dcc-303b-410b-a26d-2e7312338ba2@googlegroups.com> (permalink) |
| Subject | Re: static data for methods... |
| From | taqmcg@gmail.com |
On Wednesday, October 9, 2013 10:18:43 AM UTC-4, Andreas Leitgeb wrote:
> I'm aware that in Java, methods cannot have static data within
> them. The usual "workaround" is to place static data for a
> method in static fields of the class. However, I need the
> static fields lexically separated for the methods.
If you really want a separate name space for each function you could use the name spacing provided in nested classes.
E.g.,
class Primary {
static class Method1 {
static char someVar;
... static definitions for method1
}
static class Method2 {
... static definitions for method2
}
void method1() {
char xx = Method1.someVar;
...
}
void method2() {
...
}
}
where the association of the nested class Method1 with the method method would be a local convention. This gives you a reasonably safe name space.
It doesn't allow you to access the static variables without qualification though.
If you just don't want to have to worry about prefixing different method names on each usage of a variable you could try something like:
...
void method1() {
final Method1 statics = null;
...
localVar = statics.someVar;
...
}
...
where you define a variable of the appropriate type. Then in methods that use this convention 'statics' could be used uniformly to reference each method's static data.
Not sure whether this addresses what you want, but it does only mild violence to standard usage. While you'd be using using a variable to reference static's the name 'statics' should limit any confusion that might cause -- given that this would be a general convention within a block of code.
Good luck,
Tom McGlynn
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
static data for methods... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-09 14:18 +0000
Re: static data for methods... taqmcg@gmail.com - 2013-10-09 07:49 -0700
Re: static data for methods... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-09 15:42 +0000
Re: static data for methods... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-09 15:29 +0000
Re: static data for methods... Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-10-10 14:44 +0200
Re: static data for methods... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-10 13:13 +0000
Re: static data for methods... Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-10-10 15:44 +0200
Re: static data for methods... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-10 17:26 +0000
Re: static data for methods... Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-10-11 22:03 -0700
Re: static data for methods... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-18 15:31 +0000
csiph-web