Browsing 7239 questions and answers with Jon Skeet

Java fileds initialization order

class revi { static { i = 3; System.out.println("Hello World!"); } static int i = 15; public static void main(String[] args) { ...
Jon Skeet
people
quotationmark

This is covered in section 8.3.3 of the JLS: Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope (ยง6.3). Specifically, it is a compile-time... more 12/20/2014 5:46:46 PM

people

Java switch statement using class.getSimpleName() gives Constant express required error

I'm trying to use class.getSimpleName() for the expression of a switch however it gives me an error: Constant express required I've seen answers suggesting to change the...
Jon Skeet
people
quotationmark

Is there a way to make a switch using class.getSimpleName() without having to hardcode the class names? No. Basically, calling Class.getSimpleName() doesn't count as a compile-time constant expression, so it can't be used as a case... more 12/20/2014 12:50:53 PM

people

Is LINQ2XML Descendants working in Windows Phone 8?

I'm working on Windows Phone 8 C#/XAML .NET 4.5 Application I'm trying to select element with a given name from an XML i'm getting, but when i try using Descendants to select...
Jon Skeet
people
quotationmark

You're currently looking for the elements called thisOne which aren't in a namespace. You need to specify the something namespace... the xmlns="something" part is specifying the default namespace for this element and further descendants.... more 12/19/2014 4:06:51 PM

people

Passing params through a Func<>

I use delegates with lambda expressions instead of methods with just one line of code like: Func<int, int, int> Add = (x, y) => x + y; int Result = Add(1, 2); // 3 Now...
Jon Skeet
people
quotationmark

params isn't part of the type itself, so you can't specify a Func<string, params string[], string>. But you could declare your own delegates which are like Func but which have a parameter array as the final parameter. For... more 12/19/2014 12:25:18 PM

people

Adding Element in middle of an array

How to add an element at middle of an array? I have tried searching on google but cannot find a way to do this without looping. Can anybody help me by providing a code snippet but...
Jon Skeet
people
quotationmark

How to add an element at middle of an array? You can't add an element anywhere in an array. Once an array has been created, its size is fixed - you can modify elements, but you can't add or delete them. You could use a List<T>... more 12/19/2014 12:18:28 PM

people

Final static variable initialized in static block using method that throws exception

Consider following part of code private static class InnerClass { private static final BufferedImage connectionImage; // line A private static final int width; // other...
Jon Skeet
people
quotationmark

Basically, you should use a local variable: BufferedImage localImage = null; try { localImage = ImageIo.read(...); } catch (IOException e) { // No need to set a value, given that it's already going to be null. // You probably... more 12/19/2014 12:16:12 PM

people

Unable to call Interface methods from another class

Below is my code: public interface I1 { void method1(); } public interface I2 { void method1(); } class MyClass { static void Main(string[] args) { One...
Jon Skeet
people
quotationmark

I am unable to declare methods as Public in class One as these are Interface methods. That's only because you're using explicit interface implementation. Assuming you really need the two different implementations, you could make one... more 12/19/2014 12:11:27 PM

people

Access JSON item with variable key name

The following is my JSON data which happens to be the format supplied by Wikidata, although greatly trimmed down for clarity. { "entities": { "Q200405": { "id":...
Jon Skeet
people
quotationmark

Well if you want to put the names into string variables, just use JObject: string id = "Q200405"; // Or wherever you get that from string text = "{\"entities..."; var json = JObject.Parse(text); var claim = json["entities"][id]; string... more 12/18/2014 11:14:58 PM

people

StringBuilder initializer works one way in VS2015 but another in VS2013

I have the following line of code, with works in VS 2015 and .Net 4.0, but I am getting an error in VS 2013. StringBuilder s = new StringBuilder("test") {[0] = 'T'}; Why it...
Jon Skeet
people
quotationmark

Basically, object initializer expressions don't support indexing in C# 5 (which is what VS2013 supports). This is a new feature in C# 6. In C# 6, your code is equivalent to: StringBuilder tmp = new StringBuilder(); // Compiler-generated... more 12/18/2014 3:51:08 PM

people

Java argument is unrecognized

I'm trying to start another Java process from my jar, and I'm using a ProcessBuilder: File javaHome = new File(System.getProperty("java.home"), "bin"); List<String>...
Jon Skeet
people
quotationmark

You're passing --classpath "..." as a single argument. I believe you should specify it as two arguments: javaList.add("-classpath"); javaList.add("\\\"" + classPath + "\\\"); (It's not clear to me whether you really need all those... more 12/18/2014 3:24:34 PM

people