由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - static object assignment/init ?
相关主题
使用Clob的setString方法出现如下错误How do a Servlet sent a Applet?
请问个从windows通过jdbc连server的问题Test your PC speed
oracle help: pass array to oracle SP折腾了一天,实在是绝望了,请教请教
问一个java基础的初始化的问题,一直搞不明白 (转载)Re: 网上哪里有JDBC的入门书呢
JDK7What version of the Java Development Kit (JDKTM)
问个primitive type的问题Java向Linux迈出决定性一步
火车旅行家在中文WINDOWS下无法运行的大问题已被解决Re: JDBC question: getXXX methods
Re: Question on mouse click on image in Jdk 1.1Re: Need help with EJB
相关话题的讨论汇总
话题: static话题: notes话题: class话题: final话题: public
进入Java版参与讨论
1 (共1页)
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.

相关主题
问个primitive type的问题How do a Servlet sent a Applet?
火车旅行家在中文WINDOWS下无法运行的大问题已被解决Test your PC speed
Re: Question on mouse click on image in Jdk 1.1折腾了一天,实在是绝望了,请教请教
进入Java版参与讨论
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吧
1 (共1页)
进入Java版参与讨论
相关主题
Re: Need help with EJBJDK7
Re: JDBC, Java vs. Javascript问个primitive type的问题
Re: JDBC连接SYBASE数据库问题求助。火车旅行家在中文WINDOWS下无法运行的大问题已被解决
Re: JDBC 处理日期的问题(日期插入数据库)Re: Question on mouse click on image in Jdk 1.1
使用Clob的setString方法出现如下错误How do a Servlet sent a Applet?
请问个从windows通过jdbc连server的问题Test your PC speed
oracle help: pass array to oracle SP折腾了一天,实在是绝望了,请教请教
问一个java基础的初始化的问题,一直搞不明白 (转载)Re: 网上哪里有JDBC的入门书呢
相关话题的讨论汇总
话题: static话题: notes话题: class话题: final话题: public