Can I define multiple static blocks?
up vote
13
down vote
favorite
Can I define multiple static blocks?
If possible, why should I define muliple static blocks?
java static block
add a comment |
up vote
13
down vote
favorite
Can I define multiple static blocks?
If possible, why should I define muliple static blocks?
java static block
6
For your first question, what happens when you try?
– beny23
Apr 4 '12 at 12:45
sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
– jamesTheProgrammer
Apr 4 '12 at 12:48
add a comment |
up vote
13
down vote
favorite
up vote
13
down vote
favorite
Can I define multiple static blocks?
If possible, why should I define muliple static blocks?
java static block
Can I define multiple static blocks?
If possible, why should I define muliple static blocks?
java static block
java static block
edited Apr 4 '12 at 12:46
Matt Fenwick
31.5k13102168
31.5k13102168
asked Apr 4 '12 at 12:43
user1127214
1,20971928
1,20971928
6
For your first question, what happens when you try?
– beny23
Apr 4 '12 at 12:45
sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
– jamesTheProgrammer
Apr 4 '12 at 12:48
add a comment |
6
For your first question, what happens when you try?
– beny23
Apr 4 '12 at 12:45
sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
– jamesTheProgrammer
Apr 4 '12 at 12:48
6
6
For your first question, what happens when you try?
– beny23
Apr 4 '12 at 12:45
For your first question, what happens when you try?
– beny23
Apr 4 '12 at 12:45
sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
– jamesTheProgrammer
Apr 4 '12 at 12:48
sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
– jamesTheProgrammer
Apr 4 '12 at 12:48
add a comment |
4 Answers
4
active
oldest
votes
up vote
22
down vote
accepted
yes, you can also make multiple initialisation blocks.
This allows you to place code with the thing initialised.
private static final Map<String, String> map;
static {
// complex code to initialise map
}
private static final DbConnection conn;
static {
// handle any exceptions and initialise conn
}
add a comment |
up vote
5
down vote
public class TryInitialisation {
static int values = new int[10];
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (100.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (200.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (300.0 * i);
}
void listValues(){
for (int i=0; i<values.length; i++)
System.out.println(" " + values[i]);
}
public static void main(String args) {
TryInitialisation example = new TryInitialisation();
example.listValues();
example = new TryInitialisation(); // referencing a new object of same type
example.listValues();
}
}
here is the output:
running initialisation block
running initialisation block
running initialisation block
0
300
600
900
1200
1500
1800
2100
2400
2700
0
300
600
900
1200
1500
1800
2100
2400
2700
The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).
Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.
add a comment |
up vote
4
down vote
You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.
add a comment |
up vote
4
down vote
Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
22
down vote
accepted
yes, you can also make multiple initialisation blocks.
This allows you to place code with the thing initialised.
private static final Map<String, String> map;
static {
// complex code to initialise map
}
private static final DbConnection conn;
static {
// handle any exceptions and initialise conn
}
add a comment |
up vote
22
down vote
accepted
yes, you can also make multiple initialisation blocks.
This allows you to place code with the thing initialised.
private static final Map<String, String> map;
static {
// complex code to initialise map
}
private static final DbConnection conn;
static {
// handle any exceptions and initialise conn
}
add a comment |
up vote
22
down vote
accepted
up vote
22
down vote
accepted
yes, you can also make multiple initialisation blocks.
This allows you to place code with the thing initialised.
private static final Map<String, String> map;
static {
// complex code to initialise map
}
private static final DbConnection conn;
static {
// handle any exceptions and initialise conn
}
yes, you can also make multiple initialisation blocks.
This allows you to place code with the thing initialised.
private static final Map<String, String> map;
static {
// complex code to initialise map
}
private static final DbConnection conn;
static {
// handle any exceptions and initialise conn
}
answered Apr 4 '12 at 12:47
Peter Lawrey
437k55551952
437k55551952
add a comment |
add a comment |
up vote
5
down vote
public class TryInitialisation {
static int values = new int[10];
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (100.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (200.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (300.0 * i);
}
void listValues(){
for (int i=0; i<values.length; i++)
System.out.println(" " + values[i]);
}
public static void main(String args) {
TryInitialisation example = new TryInitialisation();
example.listValues();
example = new TryInitialisation(); // referencing a new object of same type
example.listValues();
}
}
here is the output:
running initialisation block
running initialisation block
running initialisation block
0
300
600
900
1200
1500
1800
2100
2400
2700
0
300
600
900
1200
1500
1800
2100
2400
2700
The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).
Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.
add a comment |
up vote
5
down vote
public class TryInitialisation {
static int values = new int[10];
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (100.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (200.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (300.0 * i);
}
void listValues(){
for (int i=0; i<values.length; i++)
System.out.println(" " + values[i]);
}
public static void main(String args) {
TryInitialisation example = new TryInitialisation();
example.listValues();
example = new TryInitialisation(); // referencing a new object of same type
example.listValues();
}
}
here is the output:
running initialisation block
running initialisation block
running initialisation block
0
300
600
900
1200
1500
1800
2100
2400
2700
0
300
600
900
1200
1500
1800
2100
2400
2700
The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).
Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.
add a comment |
up vote
5
down vote
up vote
5
down vote
public class TryInitialisation {
static int values = new int[10];
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (100.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (200.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (300.0 * i);
}
void listValues(){
for (int i=0; i<values.length; i++)
System.out.println(" " + values[i]);
}
public static void main(String args) {
TryInitialisation example = new TryInitialisation();
example.listValues();
example = new TryInitialisation(); // referencing a new object of same type
example.listValues();
}
}
here is the output:
running initialisation block
running initialisation block
running initialisation block
0
300
600
900
1200
1500
1800
2100
2400
2700
0
300
600
900
1200
1500
1800
2100
2400
2700
The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).
Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.
public class TryInitialisation {
static int values = new int[10];
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (100.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (200.0 * i);
}
static{
System.out.println("running initialisation block");
for (int i=0; i< values.length; i++)
values[i] = (int) (300.0 * i);
}
void listValues(){
for (int i=0; i<values.length; i++)
System.out.println(" " + values[i]);
}
public static void main(String args) {
TryInitialisation example = new TryInitialisation();
example.listValues();
example = new TryInitialisation(); // referencing a new object of same type
example.listValues();
}
}
here is the output:
running initialisation block
running initialisation block
running initialisation block
0
300
600
900
1200
1500
1800
2100
2400
2700
0
300
600
900
1200
1500
1800
2100
2400
2700
The static blocks were executed serially in the order in which they were declared and the values assigned by the first two static blocks is replaced by the final (third static block).
Also one more thing to observe is that the static initialization block(s) ran only once i.e when the class was loaded by the JVM independent of how many objects were created.
edited Nov 11 at 8:04
rimalonfire
181117
181117
answered Jun 18 '14 at 8:29
dresh
284814
284814
add a comment |
add a comment |
up vote
4
down vote
You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.
add a comment |
up vote
4
down vote
You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.
add a comment |
up vote
4
down vote
up vote
4
down vote
You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.
You can define multiple static blocks. But I don't think it is really necessary. But if you will define, then they will be executed sequentially. i mean the static block defined first will execute first and the next block will execute next.
answered Apr 4 '12 at 12:45
Chandra Sekhar
12k115691
12k115691
add a comment |
add a comment |
up vote
4
down vote
Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).
add a comment |
up vote
4
down vote
Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).
add a comment |
up vote
4
down vote
up vote
4
down vote
Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).
Yes. It is possible to define multiple static blocks in a java class. It helps in modularization of your initialization code, which in turn helps in better understanding and readable nature of the code(As peter mentioned).
answered Apr 4 '12 at 12:57
siva.pcu
411
411
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10011340%2fcan-i-define-multiple-static-blocks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
For your first question, what happens when you try?
– beny23
Apr 4 '12 at 12:45
sounds like a homework question...its your job to demonstrate rigor when asking a question(s).
– jamesTheProgrammer
Apr 4 '12 at 12:48