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