[#0x0001] initializer

  A block-formed initializer can be appended after the fields declared.
  If the fields are static, the initializer can also be static too, which means it's only executed once. If the initializer is not static, it will be executed as many times as the class constructor will be, even if the fields are static.
  The non-static fields can not be initialized in a static intializer.

//@file Bookshelf.java

class Book   
{   
    Book(int id)   
    {   
        System.out.println("This is book No." + id);   
    }   
}   
  
class Bookshelf   
{   
    // case 1:   
    /**  
    Book b1;  
    Book b2;   
    {  
        b1 = new Book(1);  
        b2 = new Book(2);  
    }  
    */  
    //output:   
    //This is book No.1   
    //This is book No.2   
    //This is book No.1   
    //This is book No.2   
  
    //case 2:   
    static Book b1;   
    static Book b2;   
    static    
    {   
        b1 = new Book(1);   
        b2 = new Book(2);   
    }   
    //output:   
    //This is book No.1   
    //This is book No.2   
  
    //case 3:   
    /**  
    static Book b1;  
    static Book b2;  
    {  
        b1 = new Book(1);  
        b2 = new Book(2);  
    }  
    */  
    //output:   
    //This is book No.1   
    //This is book No.2   
    //This is book No.1   
    //This is book No.2   
  
    //case 4:   
    /**  
    Book b1;  
    Book b2;  
    static   
    {  
        b1 = new Book(1);  
        b2 = new Book(2);  
    }  
    */  
    //output: error   
}   
  
class InitBlockTest   
{   
    public static void main(String[] arg)   
    {   
        new Bookshelf();   
        new Bookshelf();   
    }   
}

 

(9月4号补充)

  initializer不一定非要是初始化成员,在initializer内部其实是可以随便写的,像这样也可以:

class Test
{
	//static
	{
		System.out.println("pass");
	}
}

public class InitializerTest
{
	public static void main(String[] args)
	{
		new Test();
		new Test();
	}
}

//output
/*
	pass
	pass
*/

  另:在声明field的时候,常常会当场初始化field,这个称为variable intializer (更倾向于将声明field的表达式右值称为variable initializer)。根据field是否static及初始化是否调用了static方法,variable initializer有的属于class行为,有的属于object行为。static initializer完全属于class行为。

 

(2009年09月04日归纳:[#0x0023])

这段代码有问题,修改一下,MOV r0, #0x00002000 ; Initialize pointer to first number MOV r1, #9 ; Initialize counter with number of elements LDR r7, [r0] ; Load first number as largest LDR r8, [r0] ; Load first number as smallest Loop: ADD r0, r0, #4 ; Move pointer to next number LDR r2, [r0] ; Load the number in r2 CMP r7, r2 ; Compare largest with current number MOVLT r7, r2 ; If current number is smaller, update largest CMP r8, r2 ; Compare smallest with current number MOVGT r8, r2 ; If current number is larger, update smallest SUBS r1, r1, #1 ; Decrement counter BNE Loop ; Loop until all numbers are compared ; Display largest number on console MOV r0, #1 ; File descriptor for stdout LDR r1, =largest ; Address of string to be displayed MOV r2, #10 ; Length of string MOV r7, #4 ; Syscall number for write SWI 0 ; Call operating system ; Display largest number on LCD screen LDR r0, =0x40020C14 ; Address of LCD data register MOV r1, r7 ; Load largest number from r7 STR r1, [r0] ; Store the number in the LCD data register ; Display smallest number on console MOV r0, #1 ; File descriptor for stdout LDR r1, =smallest ; Address of string to be displayed MOV r2, #12 ; Length of string MOV r7, #4 ; Syscall number for write SWI 0 ; Call operating system ; Display smallest number on LCD screen LDR r0, =0x40020C14 ; Address of LCD data register MOV r1, r8 ; Load smallest number from r8 STR r1, [r0] ; Store the number in the LCD data register largest: .asciz "Largest number: %d\n" smallest: .asciz "Smallest number: %d\n"
05-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值