草庐IT

FINAL_DEFINE

全部标签

java - 没有 happens-before 的安全发布?除了 final 还有什么?

根据JCP(16.2.2.安全发布):Thishappens-beforeguaranteeisactuallyastrongerpromiseofvisibilityandorderingthanmadebysafepublication.WhenXissafelypublishedfromAtoB,thesafepublicationguaranteesvisibilityofthestateofX,butnotofthestateofothervariablesAmayhavetouched.ButifAputtingXonaqueuehappens-beforeBfetches

java - 线程安全但快速访问 "eventually final"变量?

我有一个有点像这样的服务器:classServer{privateWorkingThingworker;publicvoidinit(){runInNewThread({//thiswilltakeaboutaminuteworker=newWorkingThing();});}publicResponsehandleRequest(Requestreq){if(worker==null)thrownewIllegalStateException("Notinitedyet");returnworker.work(req);}}如您所见,有处理请求的线程和初始化服务器的线程。请求可以在

java - 使用 final 字段防止 "this"在构造过程中转义

publicclassApp{privatefinalAa;privatefinalServerserver;publicApp(){a=newA(this);//Bad,thisisescapingbeforeit'sinitialized.}@Subscribe//ThiseventfiressometimeafterAppisfinishedconstructing.publicvoidregisterStuff(RegisterEventevent){server=event.getServer();//Notpossibleduetofinalfieldandthisnotb

java - 为什么将一个final int赋值给一个byte时没有报错

这个问题在这里已经有了答案:WhycannotIaddtwobytesandgetanintandIcanaddtwofinalbytesgetabyte?(3个答案)关闭4年前。为什么会报错inti=123;byteb=i;但在这种情况下不是finalinti=123;byteb=i;

java - 如何使用 final 方法模拟一个类?

假设我有A类classA{finalStringfoo(){//..computingresult,contactingdatabase,whatever..return"somecomputedvalue";}//...andabazillionothermethods,someofthemfinal.}现在我有B类classB{StringmethodIWantToTest(Aa){Stringoutput=a.foo();//...whateverthismethoddoes,e.g.:output+="_suffix";returnoutput;}}我将如何对这个方法进行单元测

java - 如果一个类被声明为 final 是否有必要将方法声明为 final

我正在阅读一些内容,需要对最终类和方法进行一些说明。我的理解是,将一个类声明为final会阻止该类被扩展。因此,是否有必要将最终类中的方法声明为最终的?在我看来,如果类不能扩展,则没有必要将方法声明为final。 最佳答案 如果类被声明为final,则没有必要将方法声明为final,因为类已经不能被扩展。 关于java-如果一个类被声明为final是否有必要将方法声明为final,我们在StackOverflow上找到一个类似的问题: https://stac

java - 同步:为什么优先锁定一个private final静态对象而不是类的类对象?

简单问题:为什么这是首选:publicclassFoo{finalprivatestaticObjectfoo=newObject();publicstaticvoiddoSomething(){synchronized(Foo.foo){//code}}}关于这个:publicclassFoo{publicstaticvoiddoSomething(){synchronized(Foo.class){//code}}}或者这个:publicclassFoo{publicsynchronizedstaticvoiddoSomething(){//code}}?对我来说,这些看起来基本相

java - Protocol Buffer : How to define Date type?

我正在尝试编写一个原型(prototype)文件,该文件的日期字段未定义为ProtocolBuffer中的类型。我已经阅读了以下帖子,但我无法找到适合我的合适解决方案:Whatthebestwaystousedecimalsanddatetimeswithprotocolbuffers?.我正在尝试将proto文件转换为java。 最佳答案 我在链接帖子中的回答主要与protobuf-net相关;但是,由于您是从Java来的,所以我建议:保持简单。对于日期,我建议只使用时间(可能是毫秒)作为纪元(传统的是1970年1月1日)。有时,

java - 直接赋final变量和在构造函数中赋final变量有区别吗?

final变量value的这两种初始化有区别吗?classTest{finalintvalue=7;Test(){}}和classTest{finalintvalue;Test(){value=7;}}--编辑:一个更复杂的例子,涉及子类。在这种情况下,“0”会打印到标准输出,但如果我直接分配值,则会打印7。importjavax.swing.*;importjava.beans.PropertyChangeListener;classTestBoxextendsJCheckBox{finalintvalue;publicTestBox(){value=7;}publicvoidadd

java - 如何初始化循环依赖(final 字段相互引用)?

你如何初始化它:classA{finalBb;A(Bb){this.b=b;}}classB{finalAa;B(Aa){this.a=a;}}DI框架、反射、更好的设计?动机和用例(已添加):我的特定用例是简化A和B子类中的字段访问。因此,我将它们注入(inject)到派生类中的字段中以快速引用它们,而无需在每个子类中显式声明。还有关于DI的建议,对象最好是不可变的:Guicebestpracticesandanti-patterns. 最佳答案 你可以使用工厂方法classA{finalBb;A(Bb){this.b=b;}}a