[noob]static blocks query

M

mmu2643

Hi,

I was just trying to understand the use of 'static' for blocks of code
and wrote the following bit of code:

<code>
class frodo
{
static int frodoCount;

public frodo(){

boolean init;

init=false;

static {
frodoCount = 1;
init=true;
}

if(!init)
frodoCount++;

System.out.println("Count = "+frodoCount);
}
};

class gollum
{
public static void main(String[] args)
{
System.out.println("Hello World!");

frodo a = new frodo();
frodo b = new frodo();
frodo c = new frodo();
frodo d = new frodo();
}
}
</code>

But I keep getting the following compiler error:

C:\>javac gollum.java
gollum.java:11: illegal start of expression
static {
^
1 error

Can someone please explain what I am doing wrong? TIA.
 
L

Lasse Reichstein Nielsen

I was just trying to understand the use of 'static' for blocks of code
and wrote the following bit of code: ....
Can someone please explain what I am doing wrong? TIA.

Static blocks must be directly inside the class, not inside a method.
E.g.,
---
class FooSquare {
private static final int[] preCalc = new int[100];
static {
for (int i = 0; i < 100; i++) {
preCalc = i*i;
}
}

public int square(int i) {
if (i >= 0 && i < 100) {
return preCalc;
} else {
return i*i;
}
}
}
 
M

mmu2643

Lasse said:
Static blocks must be directly inside the class, not inside a method.

So how/when is the code in a static block executed?

For example:

class a {
static {
code_in_static
}

public a(){}
}

When instantiating 'a' when would the static code run? It would be
great if you could provide links to any articles that explain static
blocks. TIA.
 
T

Thomas Fritsch

So how/when is the code in a static block executed?
It is automatically called once when your class is loaded, before any
constructor or non-static method of this class is called.
For example:

class a {
static {
code_in_static
}

public a(){}
}

When instantiating 'a' when would the static code run?
It /has/ already been called then. See above.
It would be
great if you could provide links to any articles that explain static
blocks. TIA.
You find some here:
http://www.google.com/search?q=java+"+static+initializer"
 
T

Thomas G. Marshall

Roedy Green coughed up:
static blocks have to be defined outside methods. You can however
define a static class (nested class) inside a method.

see http://mindprod.com/jgloss/static.html
http://mindprod.com/jgloss/nestedclasses.html


This one seems fairly well put together:

http://www.flipcode.com/articles/article_innerclasses.shtml


By the way, Roedy, are you against having a dropdown box that changes
bracketting styles?

K&R
vs.
On next line before indent
vs.
On next line at indent level (your way)



--
Unix users who vehemently argue that the "ln" command has its arguments
reversed do not understand much about the design of the utilities. "ln
arg1 arg2" sets the arguments in the same order as "mv arg1 arg2".
Existing file argument to non-existing argument. And in fact, mv
itself is implemented as a link followed by an unlink.
 
T

Thomas G. Marshall

Roedy Green coughed up:
On Sat, 10 Sep 2005 23:58:48 GMT, "Thomas G. Marshall"



most definitely not. See http://mindprod.com/projects/scid.html

I go much further than that is allowing user configurability of how
they like to view source.

I don't see where you allow for your code to be viewed differently. I'm not
talking about any particular ide, I'm talking about your website.
 

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,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top