草庐IT

static-block

全部标签

java - 如何检索 'try' block 中被抑制的异常?

从Java7开始,我们可以使用try-with-resources语句:staticStringreadFirstLineFromFile(Stringpath)throwsIOException{try(BufferedReaderbr=newBufferedReader(newFileReader(path))){returnbr.readLine();}}如果br.readLine()和br.close()都抛出异常,readFirstLineFromFile将throwtryblock的异常(br.readLine()的异常),以及隐式finallyblock的try-with

java - 嵌套同步块(synchronized block)

假设我有下一节课:publicclassService{publicvoidtransferMoney(AccountfromAcct,AccounttoAcct,intamount){synchronized(fromAcct){synchronized(toAccount){//couldweusehereonlyonesynchronizedblock?fromAcct.credit(amount);toAccount.debit(amount);}}}}classAccount{privateintamount=0;publicvoidcredit(intsum){amount

java - 为什么遵循控制结构条件的声明需要在 block 中?

尝试编译以下内容时publicclassTest{publicvoidmethod(Stringfoo){//Thiscompilesifthecurlybracesareuncommentedif(fooinstanceofObject)//{Objectbar=(Object)foo;//}}}我得到以下错误javac-Xlint:allTest.javaTest.java:5:error:notastatementObjectbar=foo;^Test.java:5:error:';'expectedObjectbar=foo;^2errors为什么Objectbar=(Obje

java - case : static binding? 动态绑定(bind)?

我知道重载使用静态绑定(bind)而覆盖使用动态绑定(bind)。但是,如果它们混合在一起呢?根据thistutorial,为了解析方法调用,静态绑定(bind)使用类型信息,而动态绑定(bind)使用实际的对象信息。那么,下面的例子中是否发生静态绑定(bind)来确定调用哪个sort()方法?publicclassTestStaticAndDynamicBinding{@SuppressWarnings("rawtypes")publicstaticvoidmain(String[]args){Parentp=newChild();Collectionc=newHashSet();p

java - 如何从外部 block 开始在 lambda block 中重新抛出异常?

使用以下代码,voidkey(Key)throwsSomeCheckedException{}voidsupplier(Suppliers)throwsSomeCheckedException{ofNullable(s).ifPresent(s->{//|try{//|key(s.get());//|}catch(finalSomeCheckedExceptionsce){//|//sceiscomingfromkey()method//|//HowcanIthrowsceforoutermethod?//--/}});}我如何抛出sce就像method(supplier)方法抛出它一

java - 为什么static和default接口(interface)方法不能synchronize却可以strictfp?

这个问题在这里已经有了答案:Whatisthereasonwhy“synchronized”isnotallowedinJava8interfacemethods?(2个答案)关闭4年前。为什么静态和默认接口(interface)方法不能同步?人们说同步是一个实现细节。好吧,strictfp也是一个实现细节,但这并不妨碍在静态和默认接口(interface)方法上允许strictfp。默认方法是继承的,如果实现接口(interface)的类没有覆盖默认方法,那么让它已经同步可能会非常方便。我猜测synchronized(以及strictfp)不是继承的(我在这里吗?),但这并不能解释为

java - Essentials Java GUI 构建 block ? IE。在 Swing 之上形成框架的库

Swing为任何GUI应用程序提供了基本的构建block,但是构建GUI应用程序的更高层次呢?我不是在问那些在各种视觉组件上提供更多奇妙变体的任意库。我说的是每个人在创建除琐碎的GUI应用程序之外的任何东西时最终会自行构建的缺失部分。即特定于应用程序的逻辑构建的框架。例如处理数据绑定(bind)、应用程序生命周期问题或支持构建表单等常见事物。如果我们谈论的是持久性,您可以说任何人都可以使用javax.sql或java.io类编写自己的持久层-然而大多数人更愿意使用Hibernate之类的东西。因此,正如询问持久性库的人可能不是在ObjectOutputStream上寻找变体一样,我也不

java - 为什么对象类有静态 block ?

我只想知道为什么Object、String等在末尾有static{}block。Object类中的staticblock有什么用。打开cmd提示符,输入javapjava.lang.Object 最佳答案 您看到的只是所有方法和字段声明。由于静态block有点像方法,因此您只会看到静态初始化器的空声明。如果您查看OpenJDKsourcecode对于第40行的java.lang.Object,代码实际上是这样说的publicclassObject{privatestaticnativevoidregisterNatives();st

java - 我是否必须在 try-with-resources-block 中关闭已终止的流式查询结果?

这个问题在这里已经有了答案:Doterminaloperationsclosethestream?(2个答案)关闭6年前。在SpringDataJPA文档中它说关于流:AStreampotentiallywrapsunderlyingdatastorespecificresourcesandmustthereforebeclosedafterusage.YoucaneithermanuallyclosetheStreamusingtheclose()methodorbyusingaJava7try-with-resourcesblock.参见:http://docs.spring.io

java - block 捕获中的丢失异常

我运行这段代码:publicclassUser{publicstaticvoidmain(Stringargs[]){intarray[]=newint[10];inti=1;try{System.out.println("try:"+i++);System.out.println(array[10]);System.out.println("try");}catch(Exceptione){System.out.println("catch:"+i++);System.out.println(array[10]);System.out.println("catch");}finall