Why can't i declare a static variable in a static method?

S

Stefan Ram

ZelluX said:
public static void main(String[] args) {
double radius = 5;
static double PI = 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);

public class Main
{ public static void main( final java.lang.String[] args )
{ double radius = 5;
class Const { final static double PI = 3.15169; };
double area = radius * radius * Const.PI;
java.lang.System.out.println( "Area is " + area ); }}

Area is 78.79225

NB: Without the second »final« this instead yields:

Main.java:4: inner classes cannot have static declarations
class Const { static double PI = 3.15169; };
^
 
L

Lasse Reichstein Nielsen

Patricia Shanahan said:
More generally, it is a normal private field declaration, with the
single exception that it can only be referenced in one method.

This scope construction also has the advantage that it scales to
fields only referencable by *more* than one method, but not all.

private {
static int i = 0;
public int next() {
return i+1;
}
public void reset() {
i = 0;
}
}

Then again, is it really necessary to hide fields from their *own* class?

/L
 
P

Patricia Shanahan

Lasse said:
This scope construction also has the advantage that it scales to
fields only referencable by *more* than one method, but not all.

private {
static int i = 0;
public int next() {
return i+1;

"return i++;" would be more interesting.
}
public void reset() {
i = 0;
}
}

Then again, is it really necessary to hide fields from their *own* class?

That I am very unsure about. My general feeling is that it is possible
the class is too large, and a candidate for refactoring, if this is
really an issue.

For example, maybe those methods in your example should be replaced by a
nested class called something like "Counter".

Patricia
 
L

Lew

Patricia said:
"return i++;" would be more interesting.


That I am very unsure about. My general feeling is that it is possible
the class is too large, and a candidate for refactoring, if this is
really an issue.

For example, maybe those methods in your example should be replaced by a
nested class called something like "Counter".

The point is that no language will or can have every possible feature every
possible programmer could possibly ever want. What it can have, as Java does,
is a way to get every possible task done. If the idiom you crave doesn't
exist, switch languages or use the equivalent feature that does exist.
 
H

Hendrik Maryns

Roedy Green schreef:
It would behave exactly like a private static. It would be
initialised to 0/null. However, it would be declared in a method, and
any references to it outside that method would be illegal, detected an
compile time.

You would use it for example in

void myMethod ()
{
static Random wheel;
// first use init.
if( wheel == null ) wheel = new Random();

for (int i=0;i<100;i++)
{
System.out.println( wheel.nextInt( 6 ) );
}
}

You are declaring that wheel is purely for the use of myMethod, but
other than that in is like a private static.

To allow one-time initialisation would get fairly hairy. That is why I
side-stepped the issue. Perhaps inability to init is a reasonably
easily defined way is why Sun did not allow this. It would be a wart
no matter how it was handled.

Another syntax would be something like this where you declare outside
the method.

private to myMethod static Random wheel = new Random();

you could also have private instance methods too:

private to myMethod Random wheel = new Random();

Eiffel has ‘once’ methods, which seem to do exactly what you do here.
Might be something worth looking at, or referencing in the bug you made
at Sun’s.

H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFHQXpYe+7xMGD3itQRAgh6AJ9dEcJ6losjPCHVl3pdgv24EsVBaACeOIf2
V9x31fwJgiHnjS+gDzEgkwc=
=5+nN
-----END PGP SIGNATURE-----
 

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,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top