在Java JMS Spring中为方法参数声明final(Declaring final for a method parameter in Java JMS Spring)

编程入门 行业动态 更新时间:2024-10-24 22:30:18
在Java JMS Spring中为方法参数声明final(Declaring final for a method parameter in Java JMS Spring)

我在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

本文发布于:2023-07-09 02:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中为   声明   参数   方法   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!