i**p 发帖数: 902 | 1 想读点文章搞明白这种用法,用什么关键字到网上其搜索?
public class NotePadProvider extends ContentProvider {
....
private static final UriMatcher sUriMatcher;
......
static {
sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES);
sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID);
sNotesProjectionMap = new HashMap();
sNotesProjectionMap.put(Notes._ID, Notes._ID);
sNotesProjectionMap.put |
h*****0 发帖数: 4889 | 2 static initiator? 静态初始化器?
你不是已经写得很明白了吗?
【在 i**p 的大作中提到】 : 想读点文章搞明白这种用法,用什么关键字到网上其搜索? : public class NotePadProvider extends ContentProvider { : .... : private static final UriMatcher sUriMatcher; : ...... : static { : sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); : sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES); : sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID); : sNotesProjectionMap = new HashMap();
|
F****n 发帖数: 3271 | 3 Your code wont't work because you must assign a value to a final variable
when you define it.
【在 i**p 的大作中提到】 : 想读点文章搞明白这种用法,用什么关键字到网上其搜索? : public class NotePadProvider extends ContentProvider { : .... : private static final UriMatcher sUriMatcher; : ...... : static { : sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); : sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES); : sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID); : sNotesProjectionMap = new HashMap();
|
h*****0 发帖数: 4889 | 4 不是吧,final的东西可以被赋值一次的。
【在 F****n 的大作中提到】 : Your code wont't work because you must assign a value to a final variable : when you define it.
|
c******n 发帖数: 4965 | 5 static initialization block
【在 i**p 的大作中提到】 : 想读点文章搞明白这种用法,用什么关键字到网上其搜索? : public class NotePadProvider extends ContentProvider { : .... : private static final UriMatcher sUriMatcher; : ...... : static { : sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); : sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES); : sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID); : sNotesProjectionMap = new HashMap();
|
F****n 发帖数: 3271 | 6 Use an IDE and you won't make the same mistake.
【在 h*****0 的大作中提到】 : 不是吧,final的东西可以被赋值一次的。
|
h*****0 发帖数: 4889 | 7 i don't understand... i was using JCreator with JDK 1.6, everything is
perfect. i'm pretty sure that an final variable can be assigned once and
only once.
any other people here? please make a comment.
【在 F****n 的大作中提到】 : Use an IDE and you won't make the same mistake.
|
F****n 发帖数: 3271 | 8 Sorry that's my mistake.
static initializer is called when a class is loaded. That's why in case such
as JDBC, simply call
Class.forName("driverClassName")
can register the driver to DriverManager. They put driver registration code
in static initializer.
【在 h*****0 的大作中提到】 : i don't understand... i was using JCreator with JDK 1.6, everything is : perfect. i'm pretty sure that an final variable can be assigned once and : only once. : any other people here? please make a comment.
|
N*D 发帖数: 3641 | 9 java是要求在初始化前,包括static initializer或者constructor都可以
【在 F****n 的大作中提到】 : Your code wont't work because you must assign a value to a final variable : when you define it.
|
N*D 发帖数: 3641 | 10 once but before class load
【在 h*****0 的大作中提到】 : i don't understand... i was using JCreator with JDK 1.6, everything is : perfect. i'm pretty sure that an final variable can be assigned once and : only once. : any other people here? please make a comment.
|
|
|
h*****0 发帖数: 4889 | 11 never heard about such a rule... why?
【在 N*D 的大作中提到】 : once but before class load
|
h*****0 发帖数: 4889 | 12 as long as the assignment to a final variable happened in a "obvious" way,
it doesn't matter when. like:
final char x;
if (a == 1) {
x = 'a';
} else {
x = 'b';
}
【在 h*****0 的大作中提到】 : never heard about such a rule... why?
|
N*D 发帖数: 3641 | 13 我是说:
如果是class fields,it如果是static,必须在class load时initialize一次;如果是
instance,必须在instance initialze时复值一次;
local variable就是随便了,只能一次。
【在 h*****0 的大作中提到】 : never heard about such a rule... why?
|
h*****0 发帖数: 4889 | 14 好吧。这个可以理解,因为static field如果class load之后的赋值编译时是不知道的
,同样一般的field在instance被创建之后的赋值也是编译时不知道的。无法保证是一
次。
【在 N*D 的大作中提到】 : 我是说: : 如果是class fields,it如果是static,必须在class load时initialize一次;如果是 : instance,必须在instance initialze时复值一次; : local variable就是随便了,只能一次。
|
N*D 发帖数: 3641 | 15 理解万岁
【在 h*****0 的大作中提到】 : 好吧。这个可以理解,因为static field如果class load之后的赋值编译时是不知道的 : ,同样一般的field在instance被创建之后的赋值也是编译时不知道的。无法保证是一 : 次。
|
h*****0 发帖数: 4889 | 16 class可以动态load吗?如果可以,会不会有:
public class A {
public static final int x;
static {
\\ load(class B)
x = 1;
}
}
然后
public class B {
static {
A.x = 2;
}
}
这种情况会怎么样?编译时错误还是运行时错误?
【在 N*D 的大作中提到】 : 我是说: : 如果是class fields,it如果是static,必须在class load时initialize一次;如果是 : instance,必须在instance initialze时复值一次; : local variable就是随便了,只能一次。
|
N*D 发帖数: 3641 | 17 B的assign不就是第二次复值么?会compilation error吧
【在 h*****0 的大作中提到】 : class可以动态load吗?如果可以,会不会有: : public class A { : public static final int x; : static { : \\ load(class B) : x = 1; : } : } : 然后 : public class B {
|
h*****0 发帖数: 4889 | 18 对哦……有道理!
【在 N*D 的大作中提到】 : B的assign不就是第二次复值么?会compilation error吧
|
h*****0 发帖数: 4889 | 19 那这个呢:
public class A {
public static final int x;
static {
\\ load(class B)
x = 1;
}
}
然后
public class B {
public static final int y;
static {
y = A.x;
}
}
【在 N*D 的大作中提到】 : B的assign不就是第二次复值么?会compilation error吧
|