Browsing 7239 questions and answers with Jon Skeet

How should I declare an instance of an inner class of classes that implement the same interface

I have the following structure public interface I{...} public class A implements I{ .... public static class Inside{ ... //data from here are used in A. Therefore...
Jon Skeet
people
quotationmark

The problem is that your two Inside types are currently unrelated - there's no way for X.doSomeWork() to do refer to "it's this type or that type" when they're entirely different types. The fact that they're both nested classes called... more 3/22/2014 8:22:05 AM

people

Which of these Java conventions has higher priority?

Declaring final variables together at the top of the class. or Limiting scope of those variables by declaring within their respective methods.
Jon Skeet
people
quotationmark

It's not a matter of convention - it's a matter of what's logical state in the object. If a variable is logically part of the state of an object, it should be declared as a field - and yes, conventionally put at the top of the file. If... more 3/21/2014 8:14:18 PM

people

Handle generics

class Order { ... } class OrderA : Order { ... } class OrderB : Order { ... } class OrderC : Order { ... } class OrderD : Order { ... } private Order GetOrder() { ......
Jon Skeet
people
quotationmark

It sounds like you're expecting overload resolution to occur at execution time. That doesn't happen... unless you use dynamic, which may well be the simplest solution for you: dynamic order = GetOrder(orderId); HandleOrder(order); // The... more 3/21/2014 5:24:06 PM

people

Bug in random numbers in Android

TreeSet myNumbers = new TreeSet(); Random randGen = new Random(); for (int i = 1; i <= 16; i++) { // number generation here int randNum = randGen.nextInt(16 - 1) + 1; ...
Jon Skeet
people
quotationmark

You're only generating one number in the range 1-15. You're then generating subsequent numbers with just nextInt: if (myNumbers.add(randNum)) break; else randNum = randGen.nextInt(); That should be: if (myNumbers.add(randNum)) ... more 3/21/2014 4:01:20 PM

people

Distinct values from a nested list using lambda

I have a list of Documents which contains a list of DocumentGroups, and I want to select all the distinct DocumentGroups to perform an operation. Iterating through 2 loops is...
Jon Skeet
people
quotationmark

I suspect you just want a combination of [SelectMany]1 (to flatten the document list to a document group sequence) and then Distinct to get the distinct elements: var documentGroups = documentList.SelectMany(d => d.DocumentGroups) ... more 3/21/2014 3:29:36 PM

people

Converting a string array to long array

I want convert value in Info_TimeD1[] from string array to long array and store in ohh[].I found error and I can't pass this case. String[] Info_TimeD1; String[] ohh; int...
Jon Skeet
people
quotationmark

This isn't valid Java: Long Timestamp1[i] = // anything... It's not really clear what you're trying to do - if you're trying to populate a single element of an existing array, you should use: Timestamp1[i] = ... If you're trying to... more 3/21/2014 3:21:40 PM

people

How to find the Class with Reflection API

I am trying to find the type (Interface or class) of the "Class" instance. I can find isInterface() or isEnum(). But I want to check whether this is a class or not. WHY there is...
Jon Skeet
people
quotationmark

Misread the question. Given that every type in Java is either a primitive, an interface, a class, or an array, you just need: System.out.println(!b.isPrimitive() && !b.isInterface() && !b.isArray()); That treats enums as... more 3/21/2014 2:51:25 PM

people

Java 8 stream's .min() and .max(): why does this compile?

Note: this question originates from a dead link which was a previous SO question, but here goes... See this code (note: I do know that this code won't "work" and that...
Jon Skeet
people
quotationmark

Comparator is a functional interface, and Integer::max complies with that interface (after autoboxing/unboxing is taken into consideration). It takes two int values and returns an int - just as you'd expect a Comparator<Integer> to... more 3/21/2014 2:35:23 PM

people

How do I make a member visible to its subclasses (which might not be in the same package)?

I have an abstract base class with certain fields/abstract methods in it. How do I make these visible to its children, even if the children don't live in the same package? (And I...
Jon Skeet
people
quotationmark

Normaly, you would just use protected, which does work within subclasses within the restrictions in the JLS: In foo/Parent.java: package foo; public class Parent { protected int x; } In bar/Child.java: package bar; import... more 3/21/2014 2:15:24 PM

people

How to write numbers to a file and make them readable between Java and C#

I'm into a "compatibility" issue between two versions of the same program, the first one written in Java, the second it's a port in C#. My goal is to write some data to a file...
Jon Skeet
people
quotationmark

It's just an endian-ness issue. You can use my MiscUtil library to read big-endian data from .NET. However, I would strongly advise a simpler approach to both your Java and your .NET: In Java, use DataInputStream and DataOutputStream.... more 3/21/2014 1:42:39 PM

people