> > |
%META:TOPICINFO{author="guest" date="1044494100" format="1.0" version="1.1"}%
%META:TOPICPARENT{name="NiceTutorial"}%
A static method in Java is just a function (no dispatch) and it has no implicit 'this' parameter. So in Nice you can write a function instead (directly at the package level, not inside a class).
For instance instead of Java's
package java.lang;
abstract class Math
{
static int max(int x, int y)
{
return x > y ? x : y;
}
...
}
You would write in Nice:
package nice.lang;
int max(int x, int y)
{
return x > y ? x : y;
}
Similarly to static methods, the equivalent of Java's static field is a package variable.
Java Code:
package a;
class Color
{
static Color[] colors;
...
}
Nice code:
package a;
var Color[] colors;
class Color
{ ... }
One has to be aware that static variables have a great disadvantage: they do no play well with repetitive or concurrent uses of a library. Since libraries are often first written as a standalone program, this problem is often overthought. Later, the static variables have to be replaced with fields of a State object that is passed around. Therefore I think static variables should be seldom used. Now I wonder if language design can help discouraging their use. Should they be disallowed completely? Do some languages try to adress this issue?
-- DanielBonniot - 14 Jun 2002 |