Why can nsmc, local classes or anonymous classes have static members?

M

Mike Schilling

Arne said:
But the static method can not be called from a static context,
so whatever it does could be achieved by making it non static.

And regarding counting instances, then look at what Mike Schilling
considers sensible.

Perhaps I wasn't clear, becasue what I'm thinking counts instances
quite well, e.g.

class Outer
{
class Inner
{
static int count;

Inner()
{
count++;
}
}
}

"count" willl give the number of Inner.Outer instances ever created,
regardless of the value of the enclosing Inner instance. Though in
fact what I've usually wanted a static method for is when Inner needs
a cache, e.g.

class Outer
{
class Inner
{
static Map<String, Schema>schemas = new HashMap<String,
Schema>();
private Schema schema;

Inner(String namespace)
{
synchronized(schemas)
{
schema = schemas.get(namespace);
if (schema == null)
{
schema = loadSchema(namespace);
schemas.put(namespace, schema);
}
}
}
}
}

Again, I want to use the same cache regardless of the value of the
enclosing instance.
 
L

Lew

Mike said:
Perhaps I wasn't clear, becasue what I'm thinking counts instances
quite well, e.g.

class Outer
{
class Inner
{
static int count;

Inner()
{
count++;
}
}
}

"count" willl give the number of Inner.Outer instances ever created,

This is quite attainable without changing the rules, natch. While some seem
to prefer the syntax of the 'static' member being within the inner class,
there's no real disadvantage to the currently-legal idiom. We just have to
adapt our esthetic to The Way It Is in Java, and chalk it up as yet another
thing we'd have done better were it up to us, accepting that we can at least
achieve the functional equivalent if not through the means we imagine we'd
prefer. Oh, well.
regardless of the value of the enclosing Inner instance. Though in
fact what I've usually wanted a static method for is when Inner needs
a cache, e.g.

class Outer
{
class Inner
{
static Map<String, Schema>schemas = new HashMap<String,
Schema>();
private Schema schema;

Inner(String namespace)
{
synchronized(schemas)
{
schema = schemas.get(namespace);
if (schema == null)
{
schema = loadSchema(namespace);
schemas.put(namespace, schema);
}
}
}
}
}

Again, I want to use the same cache regardless of the value of the
enclosing instance.

Again, you can do that with currently-legal syntax, just not the exact way you
suggest. Oh, well.
 
A

Arne Vajhøj

Perhaps I wasn't clear, becasue what I'm thinking counts instances
quite well, e.g.

class Outer
{
class Inner
{
static int count;

Inner()
{
count++;
}
}
}

"count" willl give the number of Inner.Outer instances ever created,
regardless of the value of the enclosing Inner instance.

OK. I misunderstood what you wanted then. That is sensible
and consistent.

Arne
 
M

Mike Schilling

Lew said:
This is quite attainable without changing the rules, natch. While
some seem to prefer the syntax of the 'static' member being within
the inner class, there's no real disadvantage to the currently-legal
idiom.

True. The only downside is that it doesn't represent scoping
correctly.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top