Discussion:
how to access static instance variables defined in static initializer block
(too old to reply)
Marc B
2013-11-18 22:42:06 UTC
Permalink
Hello,

Preparing for the Java exam I have a question about static initializers.
A static initializer block can be used to initialize instance and static variables (http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html). So I tried it out and it ended up with this:

//============================================================
package LearnJava;

public class Holder{
// initialize instance varialbe
{
int int1 = 5;
System.out.println("Initializing instance variable int1: " + int1);
}
// initialize static variable
static {
int sint1 = 6;
System.out.println("Initializing static block. sint1: " + sint1);
}
// initialize instance varialbe
{
int int2 = 7;
System.out.println("Initializing instance variable int2: " + int2);
}
// initialize static varialbe
static {
int sint2 = 8;
System.out.println("Initializing static variable sint2: " + sint2);
}

int int1 = 5;

public Holder(){
}

public static void main(String[] args){
Holder h = new Holder();
//System.out.println("Running main. sint1: " + Holder.sint1);
System.out.println("Running main. int1: " + h.int1);
}
}
//============================================================

I expect to be able to refer to int1, int2 and sint1 and sint2 from an instantiated class and - in the case of the static variables - from the class directly. But this class does not compile because of line
System.out.println("Running main. sint1: " + Holder.sint1).
Also System.out.println("Running main. sint1: " + h.sint1) doesn't compile. The error is:

error: cannot find symbol
System.out.println("Running main. sint1: " + Holder.sint1);
^
symbol: variable sint1
location: class Holder
1 error

If I comment out this line it compiles and I get output

Initializing static block. sint1: 6
Initializing static variable sint2: 8
Initializing instance variable int1: 5
Initializing instance variable int2: 7
Running main. int1: 5

Q:
Why is the instance variable in the static {} block suddenly unknown? And why can the instance variable declared in {} be resolved???
Does this mean the static variable scan only be initialized outside any block?

Related: When I put static int sint1 = 6; n ieither the constructor or main() I get an Netbeans error "illegal start of expression" (the line gets underlined red and hovering over displays the error). Running te program gives a dialog "no main classes found"

Thanks,

Marc
Marc B
2013-11-18 22:51:54 UTC
Permalink
Ok,. I found an answer..
I have to _declare_ te static variables outside te static initializer block and _initialize_ them in the static initializer

static int sint2 = 8;
static {
sint2 = 8;
System.out.println("Initializing static variable sint2: " + sint2);
}

Then I can refer to Holder.sint2.

//

Still don't see why adding static int sint2 = 8; to contructor or main() doesn't work:

public Holder(){
static int sint2 = 9;
}

Marc
Marc B
2013-11-18 23:01:45 UTC
Permalink
Ok, found that answer too somewhere:

In Java, static means that it's a variable/method of a class, it belongs to the whole class but not to one of its certain objects.

This means that static keyword can be used only in a 'class scope' i.e. it doesn't have any sense inside methods.

So no 'static' keyword in any instance object like methods, just in the class initializer area as it is allocated during class creation, not instantiation.

Marc
markspace
2013-11-18 23:26:33 UTC
Permalink
Post by Marc B
So no 'static' keyword in any instance object like methods, just in
the class initializer area as it is allocated during class creation,
not instantiation.
Well, and method declarations themselves.

public class Foo {

public static void main( String... x ) {}
//^^^^^^
}

Both fields (variables) and methods can be declared static (class scope).

Foo.main(); // a valid method call.

Foo f = new foo();
f.main(); // valid but questionable, use Foo.main()
Lew
2013-11-28 00:05:00 UTC
Permalink
You would have found it in the Java Tutorials. Did you read them?
--
Lew
Roedy Green
2013-11-30 20:41:39 UTC
Permalink
On Mon, 18 Nov 2013 14:42:06 -0800 (PST), Marc B
Post by Marc B
static {
int sint1 =3D 6;
you still have to declare your variables static, even in a static
block
--
Roedy Green Canadian Mind Products http://mindprod.com
Unlike many machines, computers require no water once they are
manufactured.
Jukka Lahtinen
2013-11-30 22:23:46 UTC
Permalink
Post by Roedy Green
On Mon, 18 Nov 2013 14:42:06 -0800 (PST), Marc B
Post by Marc B
static {
int sint1 =3D 6;
you still have to declare your variables static, even in a static
block
What do you mean?
Variables defined inside a block don't exist outside that block, and
static modifier isn't even legal within a static block or a method.
--
Jukka Lahtinen
Loading...