Why are static variables considered evil?
2021-6-3 anglehua
I am a Java programmer who is new to the corporate world. Recently I've developed an application using Groovy and Java. All through the code I wrote used quite a good number of statics. I was asked by the senior technical lot to cut down on the number of statics used. I've googled about the same, an...Any way to declare an array in-line?
2021-6-3 anglehua
Let's say I have a method m() that takes an array of Strings as an argument. Is there a way I can just declare this array in-line when I make the call? i.e. Instead of: String[] strs = {"blah", "hey", "yo"}; m(strs); Can I just replace this with one line, and avoid declaring a named variable that...IntelliJ show JavaDocs tooltip on mouse over
2021-6-3 anglehua
In Eclipse, when hovering over a method, variable, etc. a tooltip is displayed with the corresponding JavaDocs. Is there such a feature in IntelliJ? For IntelliJ 13, there is a checkbox in Editor's page in IDE Settings EDIT: For IntelliJ 14, the option has been moved to Editor > General page. I...Why is my Spring @Autowired field null?
2021-6-3 anglehua
Note: This is intended to be a canonical answer for a common problem. I have a Spring @Service class (MileageFeeCalculator) that has an @Autowired field (rateService), but the field is null when I try to use it. The logs show that both the MileageFeeCalculator bean and the MileageRateService bean ar...IntelliJ inspection gives “Cannot resolve symbol” but still compiles code
2021-6-3 anglehua
Platform: IntelliJ Community Edition 10.0.3 SDK: jdk1.6.0_21 OS: Windows 7 So I have a strange situation with IntelliJ that has me completely stumped. I setup a Maven project and add log4j as a dependency in the pom.xml file. The IDEA inspections run fine and my unit tests all compile and run. I t...Java: convert List<String> to a String
2021-6-3 anglehua
JavaScript has Array.join() js>["Bill","Bob","Steve"].join(" and ") Bill and Bob and Steve Does Java have anything like this? I know I can cobble something up myself with StringBuilder: static public String join(List<String> list, String conjunction) { StringBuilder sb = new StringBuild...How do I address unchecked cast warnings?
2021-6-3 anglehua
Eclipse is giving me a warning of the following form: Type safety: Unchecked cast from Object to HashMap This is from a call to an API that I have no control over which returns Object: HashMap<String, String> getItems(javax.servlet.http.HttpSession session) { HashMap<String, String> ...How can I turn a List of Lists into a List in Java 8?
2021-6-3 anglehua
If I have a List<List<Object>>, how can I turn that into a List<Object> that contains all the objects in the same iteration order by using the features of Java 8? You can use flatMap to flatten the internal lists (after converting them to Streams) into a single Stream, and then co...What does the Java assert keyword do, and when should it be used?
2021-6-3 anglehua
What are some real life examples to understand the key role of assertions? Assertions (by way of the assert keyword) were added in Java 1.4. They are used to verify the correctness of an invariant in the code. They should never be triggered in production code, and are indicative of a bug or misus...Why do this() and super() have to be the first statement in a constructor?
2021-6-3 anglehua
Java requires that if you call this() or super() in a constructor, it must be the first statement. Why? For example: public class MyClass { public MyClass(int x) {} } public class MySubClass extends MyClass { public MySubClass(int a, int b) { int c = a + b; super(c); // COM...In Java, what is the best way to determine the size of an object?
2021-6-3 anglehua
I have an application that reads a CSV file with piles of data rows. I give the user a summary of the number of rows based on types of data, but I want to make sure that I don't read in too many rows of data and cause OutOfMemoryErrors. Each row translates into an object. Is there an easy way to ...What exactly is Spring Framework for?
2021-6-3 anglehua
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 3 years ago. ...RecyclerView onClick
2021-6-3 anglehua
Has anyone using RecyclerView found a way to set an onClickListener to items in the RecyclerView? I thought of setting a listener to each of the layouts for each item but that seems a little too much hassle I'm sure there is a way for the RecyclerView to listen for the onClick event but I can't quit...Difference between DTO, VO, POJO, JavaBeans?
2021-6-3 anglehua
Have seen some similar questions: What is the difference between a JavaBean and a POJO? What is the Difference Between POJO (Plain Old Java Object) and DTO (Data Transfer Object)? Can you also please tell me the contexts in which they are used? Or the purpose of them? JavaBeans A JavaBean is a cl...Downloading Java JDK on Linux via wget is shown license page instead
2021-6-3 anglehua
When I try to download Java from Oracle I instead end up downloading a page telling me that I need agree to the OTN license terms. Sorry! In order to download products from Oracle Technology Network you must agree to the OTN license terms. Be sure that... Your browser has "cookies" and JavaScript ...How do I make the method return type generic?
2021-6-3 anglehua
Consider this example (typical in OOP books): I have an Animal class, where each Animal can have many friends. And subclasses like Dog, Duck, Mouse etc which add specific behavior like bark(), quack() etc. Here's the Animal class: public class Animal { private Map<String,Animal> friends ...Eclipse/Java code completion not working
2021-6-3 anglehua
I've downloaded, unzipped and setup Eclipse 3.4.2 with some plugins (noteable, EPIC, Clearcase, QuantumDB, MisterQ). Now I find when I'm editing Java projects the code completion is not working. If I type String. and press ctrl+space a popup shows "No Default Proposals" and the status bar at the b...How do I parse command line arguments in Java?
2021-6-3 anglehua
What is a good way of parsing command line arguments in Java? Check these out: http://commons.apache.org/cli/ http://www.martiansoftware.com/jsap/ Or roll your own: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html For instance, this is how you use commons-cli to parse 2 string a...java.util.Date to XMLGregorianCalendar
2021-6-3 anglehua
Isn't there a convenient way of getting from a java.util.Date to a XMLGregorianCalendar? I should like to take a step back and a modern look at this 10 years old question. The classes mentioned, Date and XMLGregorianCalendar, are old now. I challenge the use of them and offer alternatives. Date wa...Is there a destructor for Java?
2021-6-3 anglehua
Is there a destructor for Java? I don't seem to be able to find any documentation on this. If there isn't, how can I achieve the same effect? To make my question more specific, I am writing an application that deals with data and the specification say that there should be a 'reset' button that bring...