static initialization

Hello Guys!

I磎 curious about the following behavior below.

public class Car {

static String name = "NULL";

static {

name = "MyCar";

}

public static void main(String[] args) {

System.out.println(Car.name);

}

}

Here, any doubts.

I receive "MyCar" as out, okay!

But, when I change the code for the following...

public class Car {

static {

name = "MyCar";

}

static String name = "NULL";

public static void main(String[] args) {

System.out.println(Car.name);

}

}

I磛e got receive "NULL" as out.

I think of this occour because of the order of the instrution line, but I磎 not right!

Thanks

Message was edited by:

ROBEA

[796 byte] By [ROBEAa] at [2007-11-27 6:09:58]
# 1
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.7
quittea at 2007-7-12 17:14:38 > top of Java-index,Java Essentials,Java Programming...
# 2

> Hello Guys!

> I磎 curious about the following behavior below.

>

> public class Car {

> static String name = "NULL";

> static {

> name = "MyCar";

> }

> public static void main(String[] args) {

> System.out.println(Car.name);

> }

> }

>

> Here, any doubts.

> I receive "MyCar" as out, okay!

>

> But, when I change the code for the following...

>

> public class Car {

> static {

> name = "MyCar";

> }

> static String name = "NULL";

> public static void main(String[] args) {

> System.out.println(Car.name);

> }

> }

>

> I磛e got receive "NULL" as out.

>

> I think of this occour because of the order of the

> instrution line, but I磎 not right!

Why are you not right?

georgemca at 2007-7-12 17:14:38 > top of Java-index,Java Essentials,Java Programming...
# 3
Because, I do not know if the order of the instruction line is responsible for this behavior.
ROBEAa at 2007-7-12 17:14:38 > top of Java-index,Java Essentials,Java Programming...
# 4
> Because, I do not know if the order of the> instruction line is responsible for this behavior.So you haven't read the link I posted yet?
quittea at 2007-7-12 17:14:38 > top of Java-index,Java Essentials,Java Programming...
# 5
> Because, I do not know if the order of the> instruction line is responsible for this behavior.Reply #1 contains the answer:<quote>The static initializers and class variable initializers are executed in textual order.</quote>
Hippolytea at 2007-7-12 17:14:38 > top of Java-index,Java Essentials,Java Programming...
# 6
> Because, I do not know if the order of the> instruction line is responsible for this behavior.Doesn't make you wrong, though. Follow the link
georgemca at 2007-7-12 17:14:38 > top of Java-index,Java Essentials,Java Programming...
# 7
what means "textual order"? I think is some instruction per each line in a program. example: instruction one... intruction two... and so on
ROBEAa at 2007-7-12 17:14:38 > top of Java-index,Java Essentials,Java Programming...
# 8
> what means "textual order"? > I think is some instruction per each line in a program.Yes, you are right. The initialisers happen in the same order as the lines of code. So inside main() what you see is the result of the last initialisation.
pbrockway2a at 2007-7-12 17:14:38 > top of Java-index,Java Essentials,Java Programming...