Browsing 7239 questions and answers with Jon Skeet

Why can't I access outer class from inner Thread class in Java?

I have these class and its nested classes (please go to the relevant line): import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Client { private...
Jon Skeet
people
quotationmark

Your nested class is a static nested class: private static class GetBinaryThread extends Thread Therefore it doesn't have an enclosing instance. Get rid of static and then it'll be an inner class, with a reference to an instance of... more 8/14/2015 8:30:32 PM

people

Java comparator interface and initializing comparator

I have a question regarding the Comparator interface. Below my class implements the interface to sort strings by length instead of by the default sort which sorts by character...
Jon Skeet
people
quotationmark

Even though I have overridden the default method, if I use Arrays.sort, it calls the default compare instead of my overridden method. Is this because I am explicitly calling the super class method? No, it's because in the example... more 8/14/2015 8:23:46 PM

people

Pattern to replace nested conditionals with guard clauses while still performing some action for the vast majority of cases but not all

I've got a method that kind follows the following pattern: void myMethod(String arg1) { SomeObject foo = getSomeObject(arg1); if(foo != null) { SomeOtherObject bar =...
Jon Skeet
people
quotationmark

One option would be to wrap all but the doNormalThing method call in another method that returns whether or not to execute doNormalThing afterwards: void myMethod(String arg1) { if (myMethodInternal(arg1)) { doNormalThing(); ... more 8/14/2015 5:41:39 PM

people

What is the difference between a variable, object, and reference?

Exactly what are the differences between variables, objects, and references? For example: they all point to some type, and they must all hold values (unless of course you have...
Jon Skeet
people
quotationmark

(Just to be clear, the explanation I'm giving here is specific to Java and C#. Don't assume it applies to other languages, although bits of it may.) I like to use an analogy of telling someone where I live. I might write my address on a... more 8/14/2015 12:33:57 PM

people

Failed to override toString method

I wrote a timer class. And I want to override its toString method. But when I call the toString method, it still returns the super implementation. (fully qualified name of the...
Jon Skeet
people
quotationmark

You're calling toString() from your anonymous inner class - the new Runnable() { ... }. That means you're calling toString() on your anonymous class instance, not on the Timer instance. I suspect you're getting a $1 in the output, showing... more 8/14/2015 12:29:40 PM

people

&& operator overloading and assignments in C# Clarification?

Following this very interesting issue which was originated from this question - I want to take 1 steps back please (removed the dynamic environment) : Looking at this code :...
Jon Skeet
people
quotationmark

Firstly, don't forget that this is deliberately bizarre code, used to find a corner case. If you ever find a type that behaves like this in a genuine program, find the author and have a quiet word with them. Still I don't understand :... more 8/14/2015 11:54:14 AM

people

How to disable "Light Bulb" Quick Action "simplification" for Nullable<T>?

For several reasons we prefer Nullable<Int32> over int?. Wherever possible we prefer Types over keywords - as we do so since C#2 we have a large codebase already using that...
Jon Skeet
people
quotationmark

In your project properties, under the Build tab, in "Errors and warnings", add IDE0001 to the set of suppressed warnings. I know this isn't really a warning (just a suggestion) but it's the same infrastructure. That removes the suggestion. more 8/14/2015 9:05:19 AM

people

java system.out.printf format numerics rounding off and spacing issue together

public class Solution { public static void main(String[] args) { String[] languages = { "java", "cpp", "python" }; int[] values = { 100, 65, 60 }; ...
Jon Skeet
people
quotationmark

The code you've got doesn't match the output you've shown - it's nearly right already. It comes up with: java 100 cpp 065 python 060 The only problem here is that you're right-padding the names to a minimum of 5 characters... but... more 8/14/2015 7:37:50 AM

people

Using generic interface as typeparameter for a method or function

Assuming the following interface which I use to define the parameter type and return type for a stored procedure... public interface IStoredProcedure<out TReturn, out...
Jon Skeet
people
quotationmark

You need to use the type arguments where you specify the interface: public static void DoAction<TReturnType, TParameterType> (IStoredProcedure<TReturnType, TParameterType> procedure1) where TReturnType : class where... more 8/14/2015 6:24:03 AM

people

Use of unassigned local variable in an if else statement that assigns it

I have the following code that parses some strings into doubles double number; double exponent; if(!double.TryParse(line1, out number) && !double.TryParse(line2, out...
Jon Skeet
people
quotationmark

I'm pretty sure you want || instead of &&. Currently, you're only throwing an exception if both lines are invalid - if the first line is valid, you're never even parsing the second. I assume you actually only want to use number and... more 8/13/2015 5:43:07 PM

people