Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2857
| Newsgroups | comp.lang.java.help |
|---|---|
| Date | 2013-11-18 14:42 -0800 |
| Message-ID | <8ed39e6c-ccea-480e-b3a3-8b802957ede8@googlegroups.com> (permalink) |
| Subject | how to access static instance variables defined in static initializer block |
| From | Marc B <marc.at.compass@gmail.com> |
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
Back to comp.lang.java.help | Previous | Next — Next in thread | Find similar | Unroll thread
how to access static instance variables defined in static initializer block Marc B <marc.at.compass@gmail.com> - 2013-11-18 14:42 -0800
Re: how to access static instance variables defined in static initializer block Marc B <marc.at.compass@gmail.com> - 2013-11-18 14:51 -0800
Re: how to access static instance variables defined in static initializer block Marc B <marc.at.compass@gmail.com> - 2013-11-18 15:01 -0800
Re: how to access static instance variables defined in static initializer block markspace <markspace@nospam.nospam> - 2013-11-18 15:26 -0800
Re: how to access static instance variables defined in static initializer block Lew <lewbloch@gmail.com> - 2013-11-27 16:05 -0800
Re: how to access static instance variables defined in static initializer block Roedy Green <see_website@mindprod.com.invalid> - 2013-11-30 12:41 -0800
Re: how to access static instance variables defined in static initializer block Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-12-01 00:23 +0200
csiph-web