Вы находитесь на странице: 1из 2

Array Reference Assignments for Multidimensional Arrays

When you assign an array to a previously declared array reference, the array you're
assigning must be the same dimension as the reference you're assigning it to. For example, a
two-dimensional array of intarrays cannot be assigned to a regular int
array reference, as follows:
int[]blots;
int[][]squeegees=newint[3][];
blots=squeegees;//NOTOK,squeegeesisa
//twodarrayofintarrays
int[]blocks=newint[6];
blots=blocks;//OK,blocksisanintarray

Pay particular attention to array assignments using different dimensions. You


might, for example, be asked if it's legal to assign an intarray to the first element in
an array of intarrays, as follows:
int[][]books=newint[3][];
int[]numbers=newint[6];
intaNumber=7;
books[0]=aNumber;//NO,expectinganintarraynotanint
books[0]=numbers;//OK,numbersisanintarray

Figure 3-6 shows an example of legal and illegal assignments for references to an array.

Initialization Blocks

We've talked about two places in a class where you can put code that performs
operations: methods and constructors. Initialization blocks are the third place in a
Java program where operations can be performed. Initialization blocks run when the
class is first loaded (a static initialization block) or when an instance is created (an
instance initialization block). Let's look at an example:
classSmallInit{
staticintx;
inty;
static{x=7;}//staticinitblock
{y=8;}//instanceinitblock
}

As you can see, the syntax for initialization blocks is pretty terse. They don't
have names, they can't take arguments, and they don't return anything. A static
initialization block runs once, when the class is first loaded. An instance initialization
block runs once every time a new instance is created. Remember when we talked about
the order in which constructor code executed? Instance init block code runs right
after the call to super()in a constructor, in other words, after all
super-constructors have run.
You can have many initialization blocks in a class. It is important to note that
unlike methods or constructors, the order in which initialization blocks appear in a class
matters. When it's time for initialization blocks to run, if a class has more than one,
they will run in the order in which they appear in the class filein other words,
from the top down. Based on the rules we just discussed, can you determine the
output of the following program?
classInit{
Init(intx){System.out.println("1argconst");}
Init(){System.out.println("noargconst");}
static{System.out.println("1ststaticinit");}
{System.out.println("1stinstanceinit");}
{System.out.println("2ndinstanceinit");}
static{System.out.println("2ndstaticinit");}
publicstaticvoidmain(String[]args){
newInit();
newInit(7);
}
}

To figure this out, remember these rules:

Init blocks execute in the order they appear.


Static init blocks run once, when the class is first loaded.
Instance init blocks run every time a class instance is created.
Instance init blocks run after the constructor's call to super().

With those rules in mind, the following output should make sense:
1ststaticinit
2ndstaticinit
1stinstanceinit
2ndinstanceinit
noargconst
1stinstanceinit
2ndinstanceinit
1argconst

As you can see, the instance init blocks each ran twice. Instance init blocks are
often used as a place to put code that all the constructors in a class should share.
That way, the code doesn't have to be duplicated across constructors.
Finally, if you make a mistake in your static init block, the JVM can throw an
ExceptionInInitializationError. Let's look at an example,
classInitError{
staticint[]x=newint[4];
static{x[4]=5;}//badarrayindex!
publicstaticvoidmain(String[]args){}
}

which produces something like:


Exceptioninthread"main"java.lang.ExceptionInInitializerError
Causedby:java.lang.ArrayIndexOutOfBoundsException:4
atInitError.<clinit>(InitError.java:3)

Вам также может понравиться