【java – 实例初始化器被认为是坏样式吗?】教程文章相关的互联网学习教程文章

java-数据类型原始对象未正确初始化布尔值【代码】

我必须创建一个包装器类来保存任何原始数据类型.我创建了该类,该类可用于除布尔值以外的所有内容.这是我的代码:byte a; // the byte in primitive short b; // the short int c, j; // int c is is int, j is the counter for Primitive to tell what dataType it is long d; // the long float e; //the float double f; // the double boolean g; // the boolean, with which I am having problems char h; // the char ...; pu...

java-使用最少的线程初始化Jvm【代码】

我有一个简单的带有睡眠的java hello world程序 Java代码:public class Main {public static void main(String[] args) throws InterruptedException {Thread.sleep(100000l);}}使用javac编译javac Main.java运行使用java Main现在,如果我在jvisualvm中看到了这11个线程.为什么启动RMI线程?如何仅使用基本线程启动jvm? 我之所以要这样做,是因为受limit.conf中nproc限制的约束,并希望同时运行maximun jvm.而且这些JVM是短暂的. 我...

java-Kotlin初始化对象【代码】

我有一个要扩展的基类,但想在普通Java构造函数所在的位置膨胀一个视图.class TextView(context: Context?) : ViewAbstractClass(context) 我不确定如何在Kotlin中进行此操作. Kotlin有哪些构造可让您执行对象的复杂初始化?解决方法:https://kotlinlang.org/docs/reference/classes.html#constructorsclass Customer(name: String) {init {logger.info("Customer initialized with value ${name}")} }

如何在不初始化Java中for循环的情况下重用变量?【代码】

我正在用Java编写一个函数,该函数计算字符串中空格后的字符数.对于某些人来说,这个问题听起来微不足道.public int countAfterSpaces(final String a){int position = 0; // escapes leading whitespaceswhile(position<a.length() && a.charAt(position)==' ') position++;现在,我想在for循环中重用此变量(位置),而不在初始化语句中创建新的变量(i).目前,我正在这样做.int count = 0;for (int i=position; i<a.length; i++) count+...

ARM64计算机上的Cassandra启动失败(java.lang.NoClassDefFoundError:无法初始化com.sun.jna.Native类)【代码】

当我尝试在运行64位oracle JDK的ARM64计算机上启动cassandra 3.9和3.7时.但是它无法从没有找到类定义错误开始. JNA似乎有问题. 但是,同样可以在ARM32机器上正常运行. 以下是cassandra的启动日志:- pi @ DietPi:?/ cassandra / apache-cassandra-3.9 / conf $../bin/cassandra -fCompilerOracle: dontinline org/apache/cassandra/db/Columns$Serializer.deserializeLargeSubset (Lorg/apache/cassandra/io/util/DataInputPlus;Lo...

java-FXMLLoader初始化和SpringBoot-@Configuration【代码】

我有MVP将Spring Boot与JavaFX集成的教程中的一个问题 GitHub的:https://github.com/mvpjava YouTube的:https://www.youtube.com/watch?v=hjeSOxi3uPg 在Spring Boot应用程序启动后,有一个名为initialize()的公共方法,我不知道如何调用它.我在类和fxml文件中到处都可以找到对它的引用,但没有发现. 我想知道如何调用此方法,因为我还想对JavaFX控件进行一些初始化.我尝试使用@PostConstruct,但是这是错误的,因为所有bean都是在创建...

java-通过List.of()初始化ArrayList【代码】

从不耐烦的Core Java中:… there is no initializer syntax for array lists. The best you cando is construct an array list like this: ArrayList<String> friends = new ArrayList<>(List.of("Peter", "Paul"));但是当我尝试编译此代码时遇到错误:error: cannot find symbolArrayList<String> friends = new ArrayList<>(List.of("Peter", "Paul"));^symbol: variable List我的进口是:import java.util.List; import ja...

在Java中初始化数组的一部分【代码】

我正在学习Java,但是我在文档中没有看到任何有关此的内容.是否可以仅用一行这样的一堆不同的字符串来初始化数组的一部分:String[] anArray = new String[20] {"water", "shovel", "berries", "stick", "stone", "seed", "axe"};数组中只有7个空格被填充,其余的可以稍后初始化吗?这可能吗?我发现手动填充每个空间anArray [i]很麻烦.由于代码中其他地方的原因,我也不想使用ArrayList. 帮助赞赏!解决方法:您可以执行以下操作,它将...

java-如何加快Tomcat SSL初始化【代码】

我正在开发Tomcat Web应用程序,因此需要能够重新启动Tomcat以加载代码更改.但是,有时Tomcat需要很长时间才能启动,因为主线程停在以下位置:java.lang.Thread.State: RUNNABLEat org.apache.tomcat.jni.SSL.initialize(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessor...

Java中String初始化了String域 和 构造器初始化的String域的差异【代码】

这是 java编程思想 第五章---初始化与清理 的一题 练习2 :创建一个类,它 包含了一个定义时就被初始化了的String域,以及另一个通过构造器初始化的String域,这两种方式有何差异. 看这一段程序以及结果, 首先明确三个知识点: 1.String是引用对象 2.equals() 比较的是值 3. "=="是比较的 地址 String s1 = "abc";String s2 = "abc";String s3 = new String("abc");String s4 = new String("abc");System.out.println(s1==s2)...

java-Spring Boot中的Firebase在初始化期间给出错误【代码】

我正在尝试在Spring Boot应用中设置Firebase.我正在遵循文档here中给出的代码片段.这就是我的pom的外观:<dependency><groupId>com.google.firebase</groupId><artifactId>firebase-admin</artifactId><version>5.2.0</version> </dependency>我运行以初始化Firebase的代码:@PostConstructpublic void init() {InputStream serviceAccount = FirebaseConfig.class.getClassLoader().getResourceAsStream(configPath);FirebaseOpti...

在Java的另一个类中实例化/初始化的对象的引用变量【代码】

我问的原因是因为我收到了NullPointerException.我现在这很容易,但是我是相当新的编程人员,发现这有点令人困惑.可以这么说,我已经在一个类中初始化了一个对象,并想从另一个类访问该对象. 像现在一样,例如,我正在开发一个小型国际象棋游戏,在我的模型Game类中,我有一个Board实例,一个对象.董事会又拥有一系列的正方形.广场[][]. 游戏有局,局有Square [] []. 现在,如果我想通过Board类型的对象面板(在游戏中)访问Square [] [].我是否...

初始化2D对象数组时,空指针异常[Java]【代码】

我正在尝试制作2D切片游戏,当制作包含切片的阵列时,我得到了NullPointerException,这是一些代码.抱歉,如果格式不正确,请第一时间 公共类世界{//holds data for where to place images and rectangles int[][] worldDat = new int[25][25]; //hold rectangles for checking interaction with player Rectangle[][] blocks = new Rectangle[25][25]; //holds block's images to be painted Image[][] blockImage = new Image[25][25...

java-重新启动自己-我可以从头开始重新初始化所有内容吗?【代码】

我有这样的事情:public static final String path; static {path = loadProperties("config.conf").getProperty("path"); }public static void main(String... args) {// ... do stuff (starting threads that reads the final path variable)// someone want's to update the path (in the config.conf file)restart(); // ??? }我想重新初始化再次调用静态初始化程序的JVM,然后重新初始化main(…)! 能做到吗解决方法:您可以使用...

JAVA初始化块【代码】

正如某些消息来源所说,Java实例初始化块在创建实例时或在构造函数之前立即执行.但是想象一下这种情况:public class Foo {{System.out.println("Foo init");}public Foo(){{System.out.println("Foo constr");}} }public class Main extends Foo {{System.out.println("Main init");}public Main(){{System.out.println("Main constr");}}public static void main(String[] args) {new Main();} }输出是(如预期的那样):Foo init F...

实例 - 相关标签