What is the common or best practice to structure the location of your exception classes? Let’s say you have the packages/namespaces myproject.person (models and DAOs for persons) and myproject.order (models and DAOs for orders) and the exceptions PersonException and OrderException. Should I put the exce…
Create a triangle out of stars using only recursion
I need to to write a method that is called like printTriangle(5);. We need to create an iterative method and a recursive method (without ANY iteration). The output needs to look like this: This code works with the iterative but I can’t adapt it to be recursive. I should note that you cannot use any clas…
XML Schema to Java Classes with XJC
I am using xjc to generate Java classes from the XML schema and the following is an excerpt of the XSD. For the most part the generated classes are fine but for the above block I would get something like: with the following comment above it: I have placed a comment at the end of the two line in question.
Get Maven artifact version at runtime
I have noticed that in a Maven artifact’s JAR, the project.version attribute is included in two files: Is there a recommended way to read this version at runtime? Answer You should not need to access Maven-specific files to get the version information of any given library/class. You can simply use getCl…
Why would an Enum implement an Interface?
I just found out that Java allows enums to implement an interface. What would be a good use case for that? Answer Enums don’t just have to represent passive sets (e.g. colours). They can represent more complex objects with functionality, and so you’re then likely to want to add further functionali…
Turning a string into a Uri in Android
I have a string, ‘songchoice’. I want it to become a ‘Uri’ so I can use with MediaPlayer.create(context, Uri) How can I convert songchoice to the Uri? Answer Here’s the doc http://developer.android.com/reference/android/net/Uri.html#parse%28java.lang.String%29
How to ignore SSL certificate errors in Apache HttpClient 4.0
How do I bypass invalid SSL certificate errors with Apache HttpClient 4.0? Answer You need to create a SSLContext with your own TrustManager and create HTTPS scheme using this context. Here is the code,
Java Inner Classes
I’m new to Java and have the following question regarding inner classes: When implementing an inner class, do I need to declare its attributes and methods scope i.e. public, private, protected? EDIT: With the absence of delegates (as in C#) could someone mention how best to implement a messaging system …
Naming convention for utility classes in Java
When writing utility classes in Java, what are some good guidelines to follow? Should packges be “util” or “utils”? Is it ClassUtil or ClassUtils? When is a class a “Helper” or a “Utility”? Utility or Utilities? Or do you use a mixture of them? The standard Java…
What is the difference between Thread.start() and Thread.run()?
Why do we call the start() method, which in turn calls the run() method? Can’t we directly make a call to run()? Please give an example where there is a difference.