草庐IT

static-variables

全部标签

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

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

java - 编译时间 : no instance(s) of type variable(s) U exist

以下语句虽然毫无意义,但在句法上是合理的。finalStreamfoobar=IntStream.empty().flatMap(x->IntStream.empty().mapToObj(y->IntStream.empty().mapToLong(z->1)));//compilationerrorhereon`z->1`但是它不编译,返回:java:incompatibletypes:badreturntypeinlambdaexpressionnoinstance(s)oftypevariable(s)Uexistsothatjava.util.stream.Streamcon

java - 根据 JLS(6.4.2。模糊)的 "obscuring"的代码示例,尤其是这个 "local variable or type can obscure a package"

你能举几个模糊的例子(代码片段)吗?我读了JLS,但我不明白这个概念。JLS没有给出代码示例。隐藏在Base类和Derived类的字段之间。阴影在字段和局部变量之间。模糊-在什么(?)和什么(?)之间旁白:有趣的是,JLS说如果从父类中隐藏相应的字段不会继承:Shadowingisdistinctfromhiding(§8.3,§8.4.8.2,§8.5,§9.3,§9.5),whichappliesonlytomemberswhichwouldotherwisebeinheritedbutarenotbecauseofadeclarationinasubclass.Shadowing

java - 将 Velocity 配置为在未定义的 $variable 上失败

当$var未定义时,Velocity是否可以配置为失败(即抛出异常)。这种“快速失败”策略将有助于我们的测试周期。 最佳答案 在Velocity1.6中,您可以将以下属性添加到您的velocity.propertiesruntime.references.strict=true编辑:此处提供完整的配置列表:http://velocity.apache.org/engine/devel/configuration.html 关于java-将Velocity配置为在未定义的$variable

java :Why the Local variable should be declared final

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:IsthereanyperformancereasontodeclaremethodparametersfinalinJava?Whywouldonemarklocalvariablesandmethodparametersas“final”inJava?我正在使用PMD查看代码违规情况。在webService方法中,我有下面的代码publicServiceRequestgetData(){Statusstatus=newStatus();//code}PMD给我的建议是,这个局部变量状态可以声明为final

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

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

java微优化: combine set of boolean instance variables to bit vector based on int

我们有一个包含很多实例的类,遇到了内存问题。因此,我们尽量减少这个类的内存需求。一种想法如下。该类有许多boolean实例变量,在天真的实现中,每个实例变量都会占用一个词。可以考虑将它们组合成一个存储在int中的微型位vector,这样它们的组合内存需求就是一个字。但我怀疑JavaVM无论如何都会进行这种优化,因此手动执行它不会获得任何额外的节省。对吧? 最佳答案 boolean值使用1个字节的内存(在热点上)。您可以使用替代方案:一个BitSet:每个boolean值大约使用1位+类本身的开销、对BitSet的引用、对BitSet

Java基础: a static function without a name,或返回类型

publicclassMain{publicstaticfinalLoggerLOGGER=Logger.getLogger(Main.class.getName());static{try{LOGGER.addHandler(newFileHandler("errors.log",true));}catch(IOExceptionex){LOGGER.log(Level.WARNING,ex.toString(),ex);}}...我想知道这个无名静态函数是关于什么的。我从未在java中看到过这样的东西(我目前正在学习)。它有什么用?它通常在什么时候使用?什么时候在程序中执行?

java - 调用函数: two times or storing the result in a variable?哪个更好

这个疑惑我也遇到过很多次,但是一直没有找到正确的解决方案。这次我要清除它。我有这样的情况1.StringsNumber="ksadfl.jksadlf";if(sNumber.lastIndexOf('.')>0)//dosomething......if(sNumber.lastIndexOf('.')>1)//dosomething...2.intindex=sNumber.lastIndexOf('.');if(index>0)//dosomething......if(index>1)//dosomething...第一种方式和第二种方式之间的权衡是什么?将结果存储在变量中或调

java - 需要不兼容的类型 : Class<T> found: Class<CAP#1> where T is a type-variable

以下代码:publicclassA{Classklass;Tinstance;publicA(Tinstance){this.klass=instance.getClass();//thisrequiresanexplicitcasttoClasstosatisfythecompilerthis.instance=instance;}}编译时给出:A.java:7:error:incompatibletypesthis.klass=instance.getClass();^required:Classfound:ClasswhereTisatype-variable:TextendsO