Does a finally block always get executed in Java?

Yes, finally will be called after the execution of the try or catch code blocks. The only times finally won’t be called are: If you invoke System.exit() If you invoke Runtime.getRuntime().halt(exitStatus) If the JVM crashes first If the JVM reaches an infinite loop (or some other non-interruptable, non-terminating statement) in the try or catch block … Read more

How can I fix ‘android.os.NetworkOnMainThreadException’?

NOTE : AsyncTask was deprecated in API level 30. AsyncTask | Android Developers This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask: class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> { private Exception exception; protected RSSFeed doInBackground(String… urls) { try { URL url = … Read more

How can I create an executable JAR with dependencies using Maven?

<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>fully.qualified.MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> and you run it with mvn clean compile assembly:single Compile goal should be added before assembly:single or otherwise the code on your own project is not included. See more details in comments. Commonly this goal is tied to … Read more

How do I test a class that has private methods, fields or inner classes?

If you have somewhat of a legacy Java application, and you’re not allowed to change the visibility of your methods, the best way to test private methods is to use reflection. Internally we’re using helpers to get/set private and private static variables as well as invoke private and private static methods. The following patterns will … Read more

Initialization of an ArrayList in one line

It would be simpler if you were to just declare it as a List – does it have to be an ArrayList? List<String> places = Arrays.asList(“Buenos Aires”, “Córdoba”, “La Plata”); Or if you have only one element: List<String> places = Collections.singletonList(“Buenos Aires”); This would mean that places is immutable (trying to change it will cause … Read more

What is a serialVersionUID and why should I use it?

The docs for java.io.Serializable are probably about as good an explanation as you’ll get: The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect … Read more

How do I convert a String to an int in Java?

String myString = “1234”; int foo = Integer.parseInt(myString); If you look at the Java documentation you’ll notice the “catch” is that this function can throw a NumberFormatException, which you can handle: int foo; try { foo = Integer.parseInt(myString); } catch (NumberFormatException e) { foo = 0; } (This treatment defaults a malformed number to 0, … Read more

When to use LinkedList over ArrayList in Java?

Summary ArrayList with ArrayDeque are preferable in many more use-cases than LinkedList. If you’re not sure — just start with ArrayList. TLDR, in ArrayList accessing an element takes constant time [O(1)] and adding an element takes O(n) time [worst case]. In LinkedList inserting an element takes O(n) time and accessing also takes O(n) time but LinkedList … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)