Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago. Improve this question What is an efficient way to implement a singleton design pattern in Java? Answer Use an enum: Joshua Bloch explained
Tag: java
How can I find the response time of a HTTP request through a Socket
I’m using a Java socket, connected to a server. If I send a HEADER http request, how can I measure the response time from the server? Must I use a provided java timer, or is there an easier way? I’m looking for a short answer, I don’t want to use other protocols etc. Obviously do I neither want to have
How to create a windows service from java app
I’ve just inherited a java application that needs to be installed as a service on XP and vista. It’s been about 8 years since I’ve used windows in any form and I’ve never had to create a service, let alone from something like a java app (I’ve got a jar for the app and a single dependency jar – log4j).
Does a finally block always get executed in Java?
Considering this code, can I be absolutely sure that the finally block always executes, no matter what something() is? Answer 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
How to get whois information of a domain name in my program?
I want to get whois information of a domain name from my c#/java programs. Is there a simple way to do this? Answer I found a perfect C# example on dotnet-snippets.com (which doesn’t exist anymore). It’s 11 lines of code to copy and paste straight into your own application.
Rule of thumb for choosing an implementation of a Java Collection?
Anyone have a good rule of thumb for choosing between different implementations of Java Collection interfaces like List, Map, or Set? For example, generally why or in what cases would I prefer to use …
How to convert a date String to a Date or Calendar object?
I have a String representation of a date that I need to create a Date or Calendar object from. I’ve looked through Date and Calendar APIs but haven’t found anything that can do this other than creating my own ugly parse method. I know there must be a way, does anyone know of a solution? Answer In brief: See SimpleDateFormat
How to generate a random alpha-numeric string
I’ve been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifier that would “likely” be unique over 500K+ generation (my needs don’t really require anything much more sophisticated). Ideally, I would be able to specify a length depending on my uniqueness needs. For example, a
Checking the results of a Factory in a unit test
I have developed some classes with similar behavior, they all implement the same interface. I implemented a factory that creates the appropriate object and returns the interface. I am writing a unit test for the factory. All you get back is an interface to the object. What is the best way to test that the factory has worked correctly? I
How can I play sound in Java?
I want to be able to play sound files in my program. Where should I look?