Change private static final field using Java reflection
2021-6-3 anglehua
I have a class with a private static final field that, unfortunately, I need to change it at run-time. Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field Is there any way to change the value? Field hack = WarpTransform2D.class.getDeclaredField...Hashset vs Treeset
2021-6-3 anglehua
I've always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I've ever known has asked me pointedly why I would use a TreeSet. From a CS background, I don't think it matters all that much which you use, and I don't care to mess around with hash functions ...How to convert an iterator to a stream?
2021-6-3 anglehua
I am looking for a concise way to convert an Iterator to a Stream or more specifically to "view" the iterator as a stream. For performance reason, I would like to avoid a copy of the iterator in a new list: Iterator<String> sourceIterator = Arrays.asList("A", "B", "C").iterator(); Collection&l...How to launch an Activity from another Application in Android
2021-6-3 anglehua
I want to launch an installed package from my Android application. I assume that it is possible using intents, but I didn't find a way of doing it. Is there a link, where to find the information? If you don't know the main activity, then the package name can be used to launch the application. Inten...MVC pattern on Android
2021-6-3 anglehua
Is it possible to implement the model–view–controller pattern in Java for Android? Or is it already implemented through Activities? Or is there a better way to implement the MVC pattern for Android? In Android you don't have MVC, but you have the following: You define your user interface in vario...Java 8 Iterable.forEach() vs foreach loop
2021-6-3 anglehua
Which of the following is better practice in Java 8? Java 8: joins.forEach(join -> mIrc.join(mSession, join)); Java 7: for (String join : joins) { mIrc.join(mSession, join); } I have lots of for loops that could be "simplified" with lambdas, but is there really any advantage of using them? ...Decode Base64 data in Java
2021-6-3 anglehua
I have an image that is Base64 encoded. What is the best way to decode that in Java? Hopefully using only the libraries included with Sun Java 6. As of v6, Java SE ships with JAXB. javax.xml.bind.DatatypeConverter has static methods that make this easy. See parseBase64Binary() and printBase64Binary...Why does this go into an infinite loop?
2021-6-3 anglehua
I have the following code: public class Tests { public static void main(String[] args) throws Exception { int x = 0; while(x<3) { x = x++; System.out.println(x); } } } We know he should have writen just x++ or x=x+1, but on x = x++ it shoul...How should I have explained the difference between an Interface and an Abstract class?
2021-6-3 anglehua
In one of my interviews, I have been asked to explain the difference between an Interface and an Abstract class. Here's my response: Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default b...When do you use Java's @Override annotation and why?
2021-6-3 anglehua
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. What are the best practices for using Java's @Override annotation and wh...Difference between jar and war in Java
2021-6-3 anglehua
What is the difference between a .jar and a .war file? Is it only the file extension or is there something more? From Java Tips: Difference between ear jar and war files: These files are simply zipped files using the java jar tool. These files are created for different purposes. Here is the ...How to escape % in String.Format?
2021-6-3 anglehua
I am storing a SQL query in my strings.xml file and I want to use String.Format to build the final string in code. The SELECT statement uses a like, something like this: SELECT Field1, Field2 FROM mytable WHERE Field1 LIKE '%something%' In order to format that I replace 'something' with %1$s so it...How to programmatically set drawableLeft on Android button?
2021-6-3 anglehua
I'm dynamically creating buttons. I styled them using XML first, and I'm trying to take the XML below and make it programattic. <Button android:id="@+id/buttonIdDoesntMatter" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="buttonName" and...What is this date format? 2011-08-12T20:17:46.384Z
2021-6-3 anglehua
I have the following date: 2011-08-12T20:17:46.384Z. What format is this? I'm trying to parse it with Java 1.4 via DateFormat.getDateInstance().parse(dateStr) and I'm getting java.text.ParseException: Unparseable date: "2011-08-12T20:17:46.384Z" I think I should be using SimpleDateFormat for pa...Resolving javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed Error?
2021-6-3 anglehua
Edit :- Tried to format the question and accepted answer in more presentable way at mine Blog Here is the original issue. I am getting this error: detailed message sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: ...IDEA: javac: source release 1.7 requires target release 1.7
2021-6-3 anglehua
When running a JUnit test, using IntelliJ IDEA, I get How can I correct this? Using SDK 1.7 Module language level is 1.7 Maven build works fine. (That's why I believe this in IDEA configuration issue) Most likely you have incorrect compiler options imported from Maven here: Also check project a...Can Mockito capture arguments of a method called multiple times?
2021-6-3 anglehua
I have a method that gets called twice, and I want to capture the argument of the second method call. Here's what I've tried: ArgumentCaptor<Foo> firstFooCaptor = ArgumentCaptor.forClass(Foo.class); ArgumentCaptor<Foo> secondFooCaptor = ArgumentCaptor.forClass(Foo.class); verify(mockBar)...Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
2021-6-3 anglehua
Several times I've been criticized for having suggested the use of the following methods: setPreferredSize setMinimumSize setMaximumSize on Swing components. I don't see any alternative to their use when I want to define proportions between displayed components. I have been told this: With layout...Getting the name of the currently executing method
2021-6-3 anglehua
Is there a way to get the name of the currently executing method in Java? Thread.currentThread().getStackTrace() will usually contain the method you’re calling it from but there are pitfalls (see Javadoc): Some virtual machines may, under some circumstances, omit one or more stack frames from the ...Best practice for REST token-based authentication with JAX-RS and Jersey
2021-6-3 anglehua
I'm looking for a way to enable token-based authentication in Jersey. I am trying not to use any particular framework. Is that possible? My plan is: A user signs up for my web service, my web service generates a token, sends it to the client, and the client will retain it. Then the client, for each ...