anonymous class explanation?

Ok, so the following piece of code compiles and run fine printing 1 2 3

. My problem is that I cant understand it thoroughly... So, obj is a reference varialbe of type object that refers to an array object...so far I think I am right... but after the " ; " I have another curly brace opening... Is that an anonymous class? Is that also referenced by " obj " ? When that curly brace closes, a " ; " seems to be optional making no difference.....Any explanations welcome. Thanks.

publicclass Test{

publicstaticvoid main(String[] args){

Object obj =newint[]{1,2,3};{

int[] someArray = (int[])obj;

for (int I:someArray)

System.out.print(I+"");

}// Wether a " ; " is placed right after this brace doesnt seem to matter

}

}

[1420 byte] By [DeChristoa] at [2007-11-26 21:57:54]
# 1

> So, obj is a

> reference varialbe of type object that refers to an

> array object...

Yes. Specifically, an array of ints

>so far I think I am right... but

> after the " ; " I have another curly brace

> opening... Is that an anonymous class?

No, it's not. The curly braces you've added around it are extraneous.

And why are you declaring obj to be an Object just to cast it back to an int[]? Why don't you just declare it as an int[]?

bckrispia at 2007-7-10 3:55:30 > top of Java-index,Java Essentials,New To Java...
# 2

There is no anonymous class in that code. You have an array initialization that is assigned to a reference variable. Following that, you have a block of code. I suspect part of your problem is that you're trying to make an anonymous class that is an array which, to my knowledge, is impossible.

kablaira at 2007-7-10 3:55:31 > top of Java-index,Java Essentials,New To Java...
# 3

> No, it's not. The curly braces you've added around

> it are extraneous.

>

> And why are you declaring obj to be an Object just to

> cast it back to an int[]? Why don't you just declare

> it as an int[]?

Thanks for the reply...so basically I can put any block of code within braces just fine... I mean doesnt seem to serve any purspose but it compiles..

DeChristoa at 2007-7-10 3:55:31 > top of Java-index,Java Essentials,New To Java...
# 4

>

> Thanks for the reply...so basically I can put any

> block of code within braces just fine...

More or less, yes.

>I mean doesnt seem to serve any purspose but it compiles..

Braces indicate scope. So a variable declared within nested braces will not be visible within the parent's nesting. Generally, this is not done.

bckrispia at 2007-7-10 3:55:31 > top of Java-index,Java Essentials,New To Java...
# 5

What you defined is a block: http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.2

I'm trying to think, but I can't remember ever seeing someone introduce

a block just for the heck of it, versus it being the body of a loop, for example.

Congratulations ;-)

DrLaszloJamfa at 2007-7-10 3:55:31 > top of Java-index,Java Essentials,New To Java...
# 6

> What you defined is a block:

> http://java.sun.com/docs/books/jls/third_edition/html/

> statements.html#14.2

>

> I'm trying to think, but I can't remember ever seeing

> someone introduce

> a block just for the heck of it, versus it being the

> body of a loop, for example.

>

> Congratulations ;-)

as usual, I've seen it in our codebase, Doctor :-(

I need a new job

georgemca at 2007-7-10 3:55:31 > top of Java-index,Java Essentials,New To Java...
# 7

How about this: back when I was teaching, I had a student who

insisted on naming his variables: variable1, variable2, variable3...

I tried to inspire him: you are programming in an object-oriented

language now, your design has tickets, sellers and buyers

(he was writing a lottery program), come up with better variable names.

When I checked, he had renamed them to object1, object2, object3,...

DrLaszloJamfa at 2007-7-10 3:55:31 > top of Java-index,Java Essentials,New To Java...
# 8
^When I was teaching, my wife asked why I always graded my exams in dark RED ink. That example illustrates my poiint.
bckrispia at 2007-7-10 3:55:31 > top of Java-index,Java Essentials,New To Java...