Browsing 7239 questions and answers with Jon Skeet

Java property values loaded in constructor are being reverted after the constructor?

I'm noticing some strange behavior. I have the following classes: public abstract class BaseFoo { public BaseFoo(String key) { Data data = Something.load( key ); ...
Jon Skeet
people
quotationmark

This is the problem with calling virtual methods in a constructor... The order of execution is: BaseFoo variable initializers BaseFoo constructor body Foo variable initializers Foo constructor body That behaviour is well documented in... more 10/18/2013 9:39:47 PM

people

Array of Monitors in C#

I'm currently trying to implement the Dining Philosopher problem, however I'm running into an issue. I declare my forks as such public static fixed Monitor forks[5]; however...
Jon Skeet
people
quotationmark

You just need to create an array of objects - you can't create an instance of Monitor; it's a static class. (I'm surprised you can even declare the array - although it's not clear why you've decided to use fixed sized buffers, either.... more 10/18/2013 5:03:51 PM

people

Looking for an explanation (documentation?) for "add(new Surface());"

I found this code online and it works for my application. I'm trying to trace through and understand it more thoroughly. I don't find any documentation that explains the use of...
Jon Skeet
people
quotationmark

Why does "super.add(new Surface()); work, but ""SampleAddMethod.add(new Surface());" fail? Because add(Component) is an instance method on Container, basically - and SampleAddMethod is a subclass of Container, indirectly. So the add... more 10/18/2013 4:24:09 PM

people

How would I invoke a Java method using a long memory address?

Lets say I had a memory address which was a long in Java, If I know that memory address was a function pointer, how could I invoke the function at this address? The reason I am...
Jon Skeet
people
quotationmark

You wouldn't, within pure Java. It's just about the opposite of a lot of what Java is about. You could do so with JNI if you really wanted to. Ideally, you'd change your design so that you didn't need to do this though - it's a pretty odd... more 10/18/2013 3:36:15 PM

people

Use of "default" as an enum in C#

I have a quick question regarding the use of "default" as an enum value. I'm reading the contents of an existing XML file and parsing the element values to enums. Here is and...
Jon Skeet
people
quotationmark

Yes, you can use it as an identifier using @: public enum LoadPolicy { @default, at_start, in_background } From the C# 5 spec, section 2.4.2: The rules for identifiers given in this section correspond exactly to those recommended by... more 10/18/2013 3:06:25 PM

people

NullPointerException in Eclipse but not in command promt

I'm a beginner. Just wondering why this code works perfectly fine in windows command prompt, but I get an: Exception in thread "main" java.lang.NullPointerException at...
Jon Skeet
people
quotationmark

Simply put, System.console() is returning null in Eclipse, but not when run in a console. This is the documented behaviour: Returns the unique Console object associated with the current Java virtual machine, if any. Returns: The... more 10/18/2013 2:29:11 PM

people

Casting WebMatrix DynamicRecord or retrieving the underlying IDictionary<string,object>

this might be an easy one but I am really not getting it. As far as I understand: The most dynamic types ins C# rely on IDictionary<string,object> I did a spike with...
Jon Skeet
people
quotationmark

The most dynamic types ins C# rely on IDictionary<string,object> Not necessarily. ExpandoObject does, but there are plenty of other ways of being dynamic, and DynamicObject doesn't. Note that reference conversion casting... more 10/18/2013 2:01:10 PM

people

Jar change outputs unicode

I have create a java applet in Netbeans IDE. My applet create a html file. That include a string. //Other code File htmlTemplateFile = new File("template.html"); String...
Jon Skeet
people
quotationmark

Your code uses FileWriter. That will always use the platform default encoding - and the detection of that may well vary between running it in Netbeans and running it as an applet. I would strongly suggest that you change the code to... more 10/18/2013 1:47:38 PM

people

Strange results when converting from byte array to string

I get strange results when converting byte array to string and then converting the string back to byte array. Try this: byte[] b = new byte[1]; b[0] = 172; string s...
Jon Skeet
people
quotationmark

Why does it happen? Because ASCII only contains values up to 127. When faced with binary data which is invalid for the given encoding, Encoding.GetString can provide a replacement character, or throw an exception. Here, it's using a... more 10/18/2013 1:36:02 PM

people

Method.Invoke() behaving strangely C# .NET BackgroundWorker

I have the following code with some detail taken out: private void bkgdProcessData_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { ...
Jon Skeet
people
quotationmark

You're calling BeginInvoke, which is asynchronous - it will execute the given delegate in the UI thread, but not block the currently executing thread... which means it's highly likely that you'll end up using the variables in the rest of... more 10/18/2013 1:10:16 PM

people