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])
分享到:
相关推荐
U盘出现DEVICE ERROR CODE 0x0406错误解决办法
LCD_WR_REG(0x0001,0x0100);//S #endif #ifdef ILI9320 LCD_WR_REG(0x00e5,0x8000); LCD_WR_REG(0x0000,0x0001); LCD_WR_REG(0x0001,0x0100);//S Delay_nms(10); #endif LCD_WR_REG(0x0002,0x0700);//Line ...
<br>这是一般修复工具的提示: <br>STEP : Log File Creation ====================================================================== STEP : Device Initialize ==============================...
- `DOWN0x50`:定义向下箭头键的键码。 - `LEFT0x4b`:定义向左箭头键的键码。 - `RIGHT0x4d`:定义向右箭头键的键码。 - `ENTER0x0d`:定义 Enter 键的键码。 ### 4. 全局变量 程序中定义了一些全局变量,用于存储...
- **AW_HOR_POSITIVE** (`0x0001`):水平向右滑动。 - **AW_HOR_NEGATIVE** (`0x0002`):水平向左滑动。 - **AW_VER_POSITIVE** (`0x0004`):垂直向上滑动。 - **AW_VER_NEGATIVE** (`0x0008`):垂直向下滑动。 - **...
Move = 0x0001, // 移动 LeftDown = 0x0002, // 左键按下 LeftUp = 0x0004, // 左键释放 RightDown = 0x0008, // 右键按下 RightUp = 0x0010, // 右键释放 MiddleDown = 0x0020, // 中键按下 MiddleUp = 0x...
movw $0x400, %sp # initialize stack ``` 这部分代码主要负责初始化段寄存器(CS、DS、SS、ES)和栈指针(SP),确保引导程序能够在正确的内存环境中运行。 ##### 2. **系统内核加载** ```assembly load_...
Primary Vendor Command Set: 0001 (Intel/Sharp Extended) Primary Algorithm Table at 0031 Alternative Vendor Command Set: 0000 (None) No Alternate Algorithm Table Vcc Minimum: 2.7V Vcc Maximum: 3.6V No ...
#define INITIALIZE (0x61) #define RUN (0x69) #define CHECK_ERROR (0x02) #define DEVICE_ADDRESS (0x01FFAFD0) ``` 这些宏定义用于设置设备的状态值,例如`TRUN_OFF`表示关闭状态,`INITIALIZE`表示初始化状态...
I_CMSTEQU 0x01e0001c ; 当前主寄存器irq优先级 ; Watchdog timer WTCONEQU 0x01d30000 ; 看门狗定时器控制寄存器 ; Clock Controller PLLCONEQU 0x01d80000 ; pll控制寄存器 CLKCONEQU 0x01d80004 ; 时钟控制寄存器...
这是一个LabVIEW版的Modbus源程序,它实现了Modbus应用协议规范V1.1b3,用于在异步串行或TCP/IP网络上与Modbus设备(从设备)通信。 支持RTU、ASCII、TCP模式,功能代码如下: ...0x2B/0x0E读取设备标识
初始化函数`initialize`负责这些工作。主循环不断检查键盘输入,并相应地更新游戏状态。 #### 6. 图形化界面 通过`<graphics.h>`提供的函数来绘制游戏界面。例如,`draw_block`函数绘制单个方块,`draw_little_...
private const int MOD_ALT = 0x0001; private const int WM_HOTKEY = 0x0312; private int hotKeyId; public MainWindow() { InitializeComponent(); // 注册全局快捷键 hotKeyId = RegisterHotKey(IntPtr....
- **用途**:定义为 `0x0d`,表示回车键的ASCII码值。 ### 3. 结构体定义 #### `struct palettetype palette` - **用途**:存储调色板信息,用于图形界面的颜色配置。 - **示例使用**: - `getpalette(&palette)`...
通过修改`initialize`方法的`requested_address`参数为一个具体的地址(例如`0x01cd0000`),我们可以显式地控制保留空间的起始地址。这样一来,在使用上述调试标志时,生成的汇编代码地址就会保持不变。 #### 五、...
r4 = 0x00000000 r5 = 0x00000000 r6 = 0x00000000 r7 = 0x00000000 r8 = 0x00000000 r9 = 0x00000000 sl = 0x00000000 fp = 0x00000000 ip = 0x00000000 sp = 0xbfffc000 lr = 0x00000000 pc = 0x000080d0 cpsr...
Contains the boot sector of the volume, which is used to initialize the file system during the boot process. ##### NTFS Files: $BadClus (8) Lists bad clusters on the volume that should not be used ...
- 如果遇到类似“Fatal error: Failed to re-initialize, Session aborted!”的错误提示,请检查项目配置是否正确。 - 重启开发板或重新插拔USB线,有时也能解决问题。 #### 六、总结 通过上述步骤,新手可以快速...
public const UInt32 SHDB_SHOW = 0x0001; public const UInt32 SHDB_HIDE = 0x0002; public const int GWL_STYLE = -16; public const UInt32 WS_NONAVDONEBUTTON = 0x00010000; /// /// 隐藏OK按钮 /// /// ...
Initialize C-variables ; HISTORY: ; 2002.02.25:kwtark: ver 0.0 ; 2002.03.20:purnnamu: Add some functions for testing STOP,POWER_OFF mode ;========================================= INCLUDE option.in...