Browsing 7239 questions and answers with Jon Skeet

Protect int, only visible to specified methods

Is there a way that i is not visible to other methods within the same class? private int i; private void updateI(int i) { this.i = i; }
Jon Skeet
people
quotationmark

No - all members of a class are visible to each other. If you find that some members of your class shouldn't be accessing a particular member, that may indicate that you should split your class up - think about whether you could partition... more 12/9/2014 1:26:21 PM

people

Can not Parse string to DateTime in Windows Phone 7

I am trying to convert the string to DateTime. But I can not convert. DateTime dt = DateTime.Parse("16/11/2014", CultureInfo.InvariantCulture); Console.WriteLine("Date==> " +...
Jon Skeet
people
quotationmark

Given that you know your input format, you should specify it with `ParseExact: DateTime dt = DateTime.ParseExact(text, "dd/MM/yyyy", CultureInfo.InvariantCulture); I would always recommend being as... more 12/9/2014 12:58:17 PM

people

Compare predicates

I have a list of predicates public List<Func<Album, bool>> Predicates { get; set; } I'd like to check if a list contains specific predicate. What I do is this...
Jon Skeet
people
quotationmark

I'm afraid the answer is basically "no". If you had expression trees instead of delegates then you could probably compare those with effort, but basically you've got references to separate methods. You'd need to inspect the IL inside the... more 12/9/2014 11:55:30 AM

people

static field initialization explanation and its requirement

After looking at so many complicated questions related to this i want to ask the explanation for following code having static field initialization. one more thing i want to know...
Jon Skeet
people
quotationmark

When a type has a static constructor, the runtime is constrained to execute all type initialization immediately before the first use of any member of the type. When it doesn't have a static constructor, the runtime has much more freedom -... more 12/9/2014 10:00:04 AM

people

JDBC Template querying

I am trying to execute this method using jdbc template: public String getClientName(String uuid) { System.out.println("UUID here in the dao layer is: " + uuid); String...
Jon Skeet
people
quotationmark

You're not using your uuid parameter anywhere... I suspect you actually want: return jdbcTemplate.query(sql, new Object[] { uuid }, new ResultSetExtractor<String>() { ... }); That way uuid will be used as the value for the... more 12/9/2014 8:26:35 AM

people

java.lang.NumberFormatException and SystemDecimalSeparator

Having this error java.lang.NumberFormatException: For input string: "20,00" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at...
Jon Skeet
people
quotationmark

You're calling Double.parseDouble, which always uses a dot as the decimal separator - it doesn't use the default locale at all. Double.parseDouble is documented to behave like Double.valueOf(String), which has documentation including: ... more 12/8/2014 7:18:40 PM

people

How to get the absolute path location for this OutputStream?

byte bWrite [] = {11,21,3,40,5}; OutputStream os = new FileOutputStream("test.txt"); System.out.println("Created successfully"+os.getAbsolutePath()); This is my code.I have to...
Jon Skeet
people
quotationmark

Getting this information after you've only got an OutputStream variable feels like the wrong approach to me. After all, there's no guarantee that an OutputStream is writing to a file at all - it could be a ByteArrayOutputStream or writing... more 12/8/2014 2:31:18 PM

people

How to avoid conditionally using the same namespace in many files

I have code like the following in many files. The code is the same in every file. #if SOMETHING using namespace1; #else using namespace2; #endif However I don't want to...
Jon Skeet
people
quotationmark

I would avoid having the same code in different namespaces twice to start with. Just generate the code once, but then wherever you create an instance of the web service, specify the URL you want to connect to. (You probably want to... more 12/8/2014 10:54:58 AM

people

Java unit testing: the easiest way to test if a callback is invoked

I often work with methods that accept callbacks, and callbacks seem to be somewhat hard to test. Let's consider the following scenario, if there's a method that accepts a callback...
Jon Skeet
people
quotationmark

Given that this is the sort of thing you're likely to need in several places, I would just create a named class to use for tests: public class FakeCallback implements Callback { private boolean wasSuccessful; private boolean... more 12/7/2014 10:31:51 PM

people

Why is Netbeans showing an error in createStatement() for a MySQL database connection?

There seems to be a problem with Statement st = conn.createStatement(); Here's my code: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { ...
Jon Skeet
people
quotationmark

The problem is that when you write Connection, the compiler thinks you mean your class, rather than java.sql.Connection... and your class doesn't have a createStatement method. All you've got to do is specify the class name... more 12/7/2014 9:02:27 PM

people