Why?Steve Dyke wrote:I am trying to use more static class methods in lieu of class constructors. . . .
Campbell Ritchie wrote:
Why?Steve Dyke wrote:I am trying to use more static class methods in lieu of class constructors. . . .
Paul Clapham wrote:No, static methods are allowed to return a value. The static method in your detail class doesn't return a value, though. Why not? It doesn't do anything else and your list class clearly expects it to return a value (hence the error message).
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Tim Holloway wrote:Be very, very, very sure that you'll never need to create more than one of them. Use a Singleton for preference.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Steve Dyke wrote:Because I was told on this forum that static class methods and properties was a more correct way to code than using the new constructor to create an object on a class where nothing changes state.
Stephan van Hulst wrote:
Math.PI is a natural constant, ... Math.sin() is a pure function
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Tim Holloway wrote:I'd recommend moving the "new operation" out of the loop and using a member function to do the work. Construction isn't a cheap operation.
Stephan van Hulst wrote:I'd write a class like this:
Obviously, if you don't want to retrieve ALL entries from the database and convert them to objects, then you should add a method that executes a more selective query.
Stephan van Hulst wrote:Obviously, if you don't want to retrieve ALL entries from the database and convert them to objects, then you should add a method that executes a more selective query.
Tim Holloway wrote:Also of note: take a look at try-with-resources.
Campbell Ritchie wrote:java.sql/java.sql.Statement does implement AutoCloseable. Please verify that you have picked up that class and not a different kind of Statement. There are several classes with similar names.
Campbell Ritchie wrote:Have you tried using the fully qualified name of the interface?Please verify that there aren't any other Statement types anywhere.
Paul Clapham wrote:I expect you're using the IBM version of the Java API? Still, try-with-resources and Autocloseable have been in the language since Java 7 and... wait, is that a compiler message or a run-time message?
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Mike Simmons wrote: What IDE are you using, if any?
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Tim Holloway wrote:Highlight the word Statement in the try-with-resources and press F4. That should open a Type Hierarchy pane and show the fully-qualified class name.
Actually, you should just be able to hover the mouse pointer over the word Statement and get its autodoc information.
Mike Simmons wrote:
My own best guess is that, while rt.jar does have the expected Statement.class file in it, there may also be some other jar file in the class path, containing an older Statement definition. I would look through other libraries being used, especially old libraries that have anything to do with database access.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Paul Clapham wrote:No, static methods are allowed to return a value. The static method in your detail class doesn't return a value, though. Why not? It doesn't do anything else and your list class clearly expects it to return a value (hence the error message).
Steve Dyke wrote:What if my detail class has multiple properties?
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Paul Clapham wrote:No, static methods are allowed to return a value.
Stephan van Hulst wrote:Return an object that encapsulates the properties.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Steve Dyke wrote:Would this be a candidate for static method(to use as a function)?
Stephan van Hulst wrote:Regardless, accessing the database from a constructor is even worse.
Stephan van Hulst wrote:Constructors should be super simple: just initialize the fields from constructor parameters and keep logic to a minimum. When I'm debugging code of my predecessor and I see a line that creates an object instance using the new keyword, I'm not expecting it to access external systems. If it does, I'm going to break into the author's house and murder them in their sleep.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Stephan van Hulst wrote:
But seriously why are you so obsessed with static methods? Stop using them. Use the repository pattern I showed you earlier.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |