What wrong with static keyword?

R

Roy M

I once heard some guy about that the "static" keyword made Java is not
a 100% object oriented language, but they don't tell the exact
rationale behind.

any get more information about this.
 
A

Arne Vajhøj

Roy said:
I once heard some guy about that the "static" keyword made Java is not
a 100% object oriented language, but they don't tell the exact
rationale behind.

A static method is not associated to an instance/object.

From a naive point of view that could be interpreted as
being procedural not object oriented.

But the reality is that there are cases in object
oriented programming that require static stuff.

Arne
 
E

Eric Sosman

Roy said:
I once heard some guy about that the "static" keyword made Java is not
a 100% object oriented language, but they don't tell the exact
rationale behind.

any get more information about this.

"Believe ten percent of what you hear, fifty percent
of what you read, and ninety percent of what you see."

Unless and until "some guy" advances an argument to
support his interpretation of the One True Way, I'd suggest
classifying him in the other ninety percent of what you hear.
(In other words, *I* feel no obligation to defend the
extravagant opinions of some unnamed religionist. If you
want to hear his opinions defended, ask him directly.)
 
L

Lew

Eric said:
"Believe ten percent of what you hear, fifty percent
of what you read, and ninety percent of what you see."

Unless and until "some guy" advances an argument to
support his interpretation of the One True Way, I'd suggest
classifying him in the other ninety percent of what you hear.
(In other words, *I* feel no obligation to defend the
extravagant opinions of some unnamed religionist. If you
want to hear his opinions defended, ask him directly.)

At issue are the definition of "object-oriented language" and "100%", and
whether 'static' members violate either of those. "Some guy" (who?)
apparently believes that "100% object oriented" requires that absolutely every
referent be a member of a class instance, and that only class instances count
as "objects". If that is his position, then by those criteria Java is,
indeed, not "100% object oriented", but that still begs the question of
whether anything is wrong with that.

However, classes are themselves objects, so 'static' members are enclosed in
objects and that therefore Java is "100%" object oriented. That still begs
the question of whether anything is wrong with 'static' members.

There are dangers associated with 'static' members to which instance members
are more immune, because 'static' members are more nearly global than instance
members. Many pundits argue that global constructs are Bad Things, so some
will tell you that 'static' members are therefore Bad Things.

Others take a more pragmatic viewpoint - that certain attributes and behaviors
do not depend on specific instance state, and therefore can reasonably assume
class scope. Consider constants:

public final class Utility
{
public static final int MAX_PERCENTAGE = 100;
}

It just isn't reasonable to demand instantiation of multiple objects every
time one needs to know the 'MAX_PERCENTAGE'; it makes much more sense to give
such a thing class scope.

Non-constant entities also are useful at 'static' scope, for example, the JPA
'EntityManagerFactory' used by an application:

public final class JpaUtility
{
public static final PUNIT = "punit";
public static final EntityManagerFactory EMF =
Persistence.createEntityManagerFactory( punit );
}

For a given data store with given connection parameters (i.e., for a given
persistence unit), there's often no real need to have more than one entity
manager factory, so it's suitable to define the factory as 'static'.
(However, 'EntityManager' instances should not be 'static'.)

There is a grey area between what clearly should be 'static' and what clearly
should not be. Opinions differ, but personally I tend toward non-'static'
members when there is ambiguity.

One must be careful when defining mutable 'static' members, especially when
multiple threads are involved. The same is true for instance members, but the
latter are easier to control. If client A modifies a member, then client B
modifies the same member, then client A reads the member, there can be confusion.
 
K

Kevin McMurtrie

Roy M said:
I once heard some guy about that the "static" keyword made Java is not
a 100% object oriented language, but they don't tell the exact
rationale behind.

any get more information about this.

And there are some that say that Object Oriented design is crap because
it doesn't follow mathematical models. Not everything people say makes
sense.

Any design pattern turns into an anti-pattern when applied for religious
reasons rather than logical reasons. Try different ways and learn to
use what works best for different situations. Some situations have only
a single stateless solution and they do just fine with "static" in the
declaration. Math.min(), String.valueOf(), etc.
 
A

alexandre_paterson

I once heard some guy about that the "static" keyword made Java is not
a 100% object oriented language, but they don't tell the exact
rationale behind.

any get more information about this.

Java is not 100% object oriented, that is a fact. But it hasn't much
to do with the static keyword.

Java allows primitives to be part of a method's signature. Anyone
suggesting that this is "100% OO" --no matter the definition of OO--
should post to comp.object for a good laugh :)

My take on it is that a solution to any customer's problem can be
modelled without ever using the static keyword in Java.

In OO --once again no matter the definition-- you'd never ever
use a static reference referencing a mutable object shared between
several objects to do communication between these objects. That
is just at the opposite end of the OO spectrum, no matter the
definition of OO. That's simply not how message passing works in OO.

However the 'static' keyword in Java as its use.

But when you're solving a problem, you're not just dealing with the
customer's problem but also with computational space related
problems. Limitations of mechanical computations are well known
and you have to overcome them. Extreme slowness, for example,
is a hard reality and in a great many program you need to implement
caches.

Caches can (not necessarily always should, I just wrote 'can')
trivially
be implemented using the static keyword.

A factory method being static is also perfectly fine. Usually, a
static "factory method" is much better than a public constructor,
for they tend not to break encapsulation as much as public
constructors.

Statics are also fine to deal with cross-cutting concerns like
logging.

A shared immutable 'constant' (e.g. MAX_PERCENTAGE) is fine with me.

A shared immutable reference to an immutable object is perfectly
fine with me.

Static mutable objects/primitives are fine as long as it's only to
deal with computational space related problem (including 'problems'
specific to the way Java does [or forces you to] do things).

But static mutable objects/primitives shared between objects related
to the business logic used to do communication between other objects
is an utterly broken way of doing OOP.

Once again, that's not how message passing is supposed to be done
in OO, no matter the definition of OO.

Anyone can disagree with this, but if anyone states that using
Java static is an OO way to do messages-passing between objects
then we've been to school on different planets and I'm not
interested in participating in a discussion with such a person ;)

Once again, for such an opinion I suggest a post to comp.object
for a good laugh :)

Peace,

Alex
 
A

Arved Sandstrom

Java is not 100% object oriented, that is a fact. But it hasn't much
to do with the static keyword.

Java allows primitives to be part of a method's signature. Anyone
suggesting that this is "100% OO" --no matter the definition of OO--
should post to comp.object for a good laugh :)

That's where the gods of OOP hang out, is it? Do you need to write an
exam before you're allowed to move up to it from a plebeian OO-language
specific NG?
My take on it is that a solution to any customer's problem can be
modelled without ever using the static keyword in Java.

Likely true, if you want to jump through hoops. It's rather telling that
every language (that I'm aware of) that lies on the OO spectrum makes
some facility available for having shared data (and/or shared methods)
available to object instances. Apart from Java and C++, Smalltalk, Ada,
Eiffel etc all have _something_ that qualifies in this regard.
In OO --once again no matter the definition-- you'd never ever
use a static reference referencing a mutable object shared between
several objects to do communication between these objects. That
is just at the opposite end of the OO spectrum, no matter the
definition of OO. That's simply not how message passing works in OO.

However the 'static' keyword in Java as its use.
[ SNIP ]
But static mutable objects/primitives shared between objects related
to the business logic used to do communication between other objects
is an utterly broken way of doing OOP.

Once again, that's not how message passing is supposed to be done
in OO, no matter the definition of OO.

Anyone can disagree with this, but if anyone states that using
Java static is an OO way to do messages-passing between objects
then we've been to school on different planets and I'm not
interested in participating in a discussion with such a person ;)

Once again, for such an opinion I suggest a post to comp.object
for a good laugh :)

You made a point of saying that message passing between Java object
instances using Java static is bad, then you explained all the ways in
which the use of static in Java is useful, then you excoriated the use
of Java static for communication between objects...again. In fact you
pretty much threw down a gauntlet. You know, you could've just said,
practical OO languages are impure, and nobody would have disagreed.

Believe it or not, but us lowly slobs on CLJP are somewhat aware of how
Java static ought to be used.

AHS
 
R

Roedy Green

I once heard some guy about that the "static" keyword made Java is not
a 100% object oriented language, but they don't tell the exact
rationale behind.

I think what they were thinking of is some other languages where
classes and the code for the methods are ordinary objects. In Java
classes are objects, but not all the information about a class is part
of the class object.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.
 
A

alexandre_paterson

That's where the gods of OOP hang out, is it?

Man, chill down I used smileys everytime I talked about a good
laugh on comp.object :)

But actually, yup, there used to be some famous OO figures posting
there.

My opinion (but it's just an opinion, so don't feel attacked :) is
that there are more "gods of OOP" [sic] on comp.object than "gods of
Java"
[paraphrasing] in c.l.j.p.

My c.l.j.p. "god of Java" left c.l.j.p. a long time ago: an IBM JVM
engineer who's now one of the "gods of Scala" [paraphrasing] world
(for he wrote scalaz)

:(

You made a point of saying that message passing between Java object
instances using Java static is bad, ...

Bad only for objects related to the customer's problem space.

When you're doing OOA issues related to the limitation of mechanical
devices are not a concern. Hence low-level hackery related to the
inner details of the machine and the language have no place in a
correct OO design.

A correct OOD does NOT use static mutable objects to do message
passing,
that's what I said.

... then you explained all the ways in
which the use of static in Java is useful,

Only for objects related to the computational space, and I precised
it was no necessity, just convenient.

I explained the OP how you could entirely model a clean solution
without ever relying on the static keyword for anything related
to the customer's problem space yet using the static keyword to solve
computational (and Java impurity) and cross-cutting issues.

I never said it was mandatory to use the static keyword to implement
a cache nor a logger, just that it was convenient (and I insisted
on that).

You know, you could've just said,
practical OO languages are impure, and nobody would have disagreed.

A one-liner response "OO languages are impure" to the OP would have
been very helpful :)

Believe it or not, but us lowly slobs on CLJP are somewhat aware of how
Java static ought to be used.

I was responding to Roy M, the OP :)
 
A

Arved Sandstrom

Man, chill down I used smileys everytime I talked about a good
laugh on comp.object :)

Not to worry, I wasn't exactly foaming at the mouth. :)
But actually, yup, there used to be some famous OO figures posting
there.

I don't doubt it. "Famous" is a good word, since it simply means "well
or widely known". I'm not suggesting that a goodly portion of these
famous OO types weren't also knowledgeable, but believe you me, I'm well
past the stage where I automatically equate "well known" or
"prolifically published" with "expert".
My opinion (but it's just an opinion, so don't feel attacked :) is
that there are more "gods of OOP" [sic] on comp.object than "gods of
Java"
[paraphrasing] in c.l.j.p.

We've got a fair few competent programmers on CLJP. There's maybe half a
dozen Java people that I myself would deify, and they don't make a habit
of posting here, true. OTOH I'm not prepared to say that even their
opinions would necessarily carry any more weight on any specific topic -
it depends on the topic.

[ SNIP ]
A one-liner response "OO languages are impure" to the OP would have
been very helpful :)

OK, OK, maybe a bit more detail didn't hurt.
I was responding to Roy M, the OP :)

Fair enough. I have to admit that I was basing my initial response on
how I use static in Java. My limited uses of it are usually functional -
stateless calls that return the same result given the same inputs. In
other words, nothing that I'd bother to model with objects.

I'll add this - there's no reason to believe that the OP is necessarily
inexperienced with Java. All it requires is that someone pronouncing on
a subject has more _perceived_ authority than the reader or listener.
Often enough that perceived authority rests on nothing more than the
fact that they bothered to write up an article and got it
published...the old axiom of "it's in print so it must be true". It's
entirely possible that the OP is a competent Java programmer who just
happened to read some facile tripe by a self-acclaimed expert.

AHS
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top