Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > [noob]static blocks query

Reply
Thread Tools

[noob]static blocks query

 
 
mmu2643@gmail.com
Guest
Posts: n/a
 
      09-10-2005
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.

 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-10-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> 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*i;
}
}

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

A more practical example is ofcourse possible

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
mmu2643@gmail.com
Guest
Posts: n/a
 
      09-10-2005
Lasse Reichstein Nielsen wrote:
> (E-Mail Removed) writes:
>
> > 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.


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.

 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      09-10-2005
<(E-Mail Removed)> wrote
> 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+...initializer%22

--
"TFritsch$t-online:de".replace(':','.').replace('$','@')


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-10-2005
On 10 Sep 2005 12:19:05 -0700, (E-Mail Removed) wrote or quoted :

>gollum.java:11: illegal start of expression
> static {


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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-10-2005
On 10 Sep 2005 12:55:00 -0700, (E-Mail Removed) wrote or quoted :

>When instantiating 'a' when would the static code run? I

see http://mindprod.com/jgloss/initialisation.html

Then my guess is you will have followon question that is answered in

http://mindprod.com/jgloss/constructor.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-10-2005
(E-Mail Removed) writes:

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


When the class is loaded, just like initalizers of static fields.

E.g.:

class A {
static int a = 42;
}

is equivalent to

class A {
static int a;
static {
a = 42;
}
}


> When instantiating 'a' when would the static code run?


Far earlier, when the class was first loaded.

See
<URL:http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.7>

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      09-10-2005
Roedy Green coughed up:
> On 10 Sep 2005 12:19:05 -0700, (E-Mail Removed) wrote or quoted :
>
>> gollum.java:11: illegal start of expression
>> static {

>
> 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/art...rclasses.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.


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-11-2005
On Sat, 10 Sep 2005 23:58:48 GMT, "Thomas G. Marshall"
<(E-Mail Removed). com> wrote or quoted
:

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


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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      09-11-2005
Roedy Green coughed up:
> On Sat, 10 Sep 2005 23:58:48 GMT, "Thomas G. Marshall"
> <(E-Mail Removed). com> wrote or quoted
>>

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

>
> 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.


--
"Realtor" and "realty" are pronounced "reel'-tor" and
"reel'-tee", *not* "reel'-a-tor" and "reel'-i-tee" !!!!
If you pronounce them with the extra syllable, you will
sound like a complete idiot.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Methods and blocks - not that clear when blocks passed into Steven Taylor Ruby 9 04-27-2009 08:46 AM
Trying to query the Address table data of AdventureWorks database from Query Analyzer - need help! Learner ASP .Net 1 01-30-2006 08:58 PM
"Building Blocks" are "Application Blocks" Arjen ASP .Net 3 02-27-2005 01:06 AM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM



Advertisments