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 ==============================...
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...
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...
// Initialize SPI FIFO registers SpiaRegs.SPIFFTX.all=0xE040; SpiaRegs.SPIFFRX.all=0x2044; SpiaRegs.SPIFFCT.all=0x0; } void spi_init() { SpiaRegs.SPICCR.all =0x000F; // Reset on, rising edge, 16-...
void initialize() //LCD初始化函数 { unsigned char num; lcden=0; write_com(0x38); //设置16x2显示,5x7点阵显示,8位数据接口 write_com(0x0c); //00001DCB,D(开关显示),C(是否显示光标),B(光标闪烁,...
调通sina33下的AP6212A0(WIFI+BT) 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 ...完成时间:2017/6/26 11:01 版本:V1.1 本文参照: 《A33 wifi移植说明书.pdf》 还有就是全志R16的parrotv1.1的官方SDK(Android...
CACHE_setL2Queue(0x3, 0x7); CACHE_setL2Queue(0x1, 0x7); CACHE_setPriL2Req(CACHE_L2PRIHIGH); /* Initialize ACPY, DMAN */ ACPY2_6X1X_init(); DMAN_init(); DMAN_setup(intHeap); /* initialize RF...
Initialize C-variables ;========================================= ;汇编不能使用include包含头文件,所有用Get ;汇编也不认识*.h 文件,所有只能用*.inc GET option.inc ;定义芯片相关的配置 GET memcfg.inc ...
程序清单L1.1 ;*******************************************...I_CMST EQU 0x01e0001c ;**************************************************************************** ;看门狗定时器预定义 WTCON EQU 0x01d30000
#define NC_CLIENT_CONNECT 0x0001 #define NC_CLIENT_DISCONNECT 0x0002 #define NC_TRANSMIT 0x0003 #define NC_RECEIVE 0x0004 #define NC_RECEIVE_COMPLETE 0x0005 // 完整接收 class CLock { public: ...
java控制串口发送数据时在windows环境下正常,linux环境下报错C [librxtxSerial.so+0x75da] Java_gnu_io_RXTXPort_nativeDrain+0xea,经多方查询验证为rxtx包版本问题,用本文档中的版本可以正常运行
while((Busy_Check()&0x80)==0x80); RS=0; RW=0; EN=0; P0=cmd; EN=1; Delayms(1); EN=0; } //写数据函数 void Write_LCD_Data(uchar dat) { while((Busy_Check()&0x80)==0x80); RS=1; RW=0; EN=0; P0=...
调通sina33下的AP6212A0版本的BT 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 ...完成时间:2017/6/26 11:01 版本:V1.0 本文参照: 《A33 wifi移植说明书.pdf》 还有就是全志R16的parrotv1.1的官方SDK(Android...
initialize(); while(1) { dutycycleAA = dutycycleA; dutycycleBB = dutycycleB; // if (dutycycleA) //控制舵机时防跑偏 // { // SW_R=0x01f4; // } // if (dutycycleA>=...
Amligic平台img烧录工具,USB_Burning_Tool_v2.1.2.rar
// Initialize Structure spid.Proportion = 10; // Set PID Coefficients spid.Integral = 8; spid.Derivative =6; spid.SetPoint = 100; // Set PID Setpoint while(1) { if(plus==0) { EA=0; for(a=0;...
Initialize_LCD(); while(1) { key=keyscan(); if(key != 0) { if(key == 'C') //清除键C按下 { clear_lcd(); i=0; flag_operator = 0; flag_equ = 0; value = 0; value_H =0; value_L = 0; flag_key = 0; flag_...