我在Spring for JMS中遇到过以下方法。 该类基本上向ActiveMQ(我的Message Queue Server)发送消息,它使用以下方法:
public void sendMessage(final String message) { this.jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); }我的问题是声明String final类型的方法参数message有什么意义?
I have come across the following method in Spring for JMS. The class basically sends a message to ActiveMQ (my Message Queue Server) and it uses the following method:
public void sendMessage(final String message) { this.jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); }My question is what is the point of declaring the method parameter message of type String final?
最满意答案
在匿名课堂上。 即使在sendMessage返回并且堆栈上的参数和局部变量不再存在之后,也会使用MessageCreator message子节点。
实际上,变量的副本是在MessageCreator子项中创建的。 并且java设计者认为将两个变量置于同一个名称后面是明智的; 否则他们需要一些复制同步。
因此,参数和局部变量必须是最终的。
In the anonymous class. child of MessageCreator message is used, even after sendMessage returned and parameter and local variables on the stack are no longer there.
So actually a copy of the variable is made in the MessageCreator child. And the java designers thought it wise to make both variables behind the same name final; otherwise they would need some synchronisation of copying.
Hence parameters and local variables need to be final.
更多推荐
message,method,方法,Message,public,电脑培训,计算机培训,IT培训"/> <meta name=&q
发布评论