Is it wrong to have static and non-static methods in the same class?

Is it wrong to have static and non-static methods in the same class?


Not really in regular java programming.

But if you're working extensively with dependency injection you probably have few or no static methods at all. In such a context it's fairly common to have only a few utility classes with static methods, and no other static methods.

No, it's not wrong. For example, a common use is to have static factory methods in a class definition.

I think it's fine to create static utils classes, especially when you're not really sure (yet) what the design should be, because you're still learning about the problem domain.

Staticness is a "yet to designed properly" marker. Often the static solution is perfectly adequate; but sometimes, as the project progresses, you find you do have to rewrite that "whole part", but you have (at that later stage) a far more complete understanding of the problem domain, and are therefore in a position to actually design a "proper solution" to those problems.

I think us programmers hammer ourselves unfairly about "rework". You need to do the work in order to understand the work well enough to do the work properly. I see no way past this catch 22;

I can cite many examples of static from the core API. java.lang.Math, java.util.Arrays, java.util.Collections. BUT please note that these classes are "utils classes" which exist only to provide a bunch of static methods. IMHO, The presence of static methods in a "stateful object" is just begging to be refactored.

I'll betcha that todays API designers would love to be able to split-down Integer (and the other wrapper classes)... BUT they're well and truly stuck with what they've got. Which is a warning in itself... that static implies final, and there's a darn good reason that (unlike C++) java methods can be overridden by default. Static is inherently more "binding" than non-static... down the trick you CAN NOT adapt implementations to different situations, contexts, etc, etc, etc.

Cheers. Keith.

I believe its a bad idea to have static and non-static methods in the same class with the same name. This can be very confusing. I would suggest trying to make the names different in this case.

Similarly, don't make methods with the same name but different case, or methods with the same name as the class in the same class. Both are legal, and even occur in the JDK, but are confusing IHMO.

I guess not especially when dealing with singletons.

Category:java Time:2009-05-16 Views:1

Related post

  • Singleton instance declared as static variable of GetInstance method 2009-01-16

    I've seen implementations of Singleton patterns where instance variable was declared as static variable in GetInstance method. Like this: SomeBaseClass &SomeClass::GetInstance() { static SomeClass instance; return instance; } I see following posi

  • How to decide between C# static and non-static methods? 2009-04-28

    [Edit] My original-question was "Why to decide between static and non-static? Both do the same..." Unfortunately it was edited to a C#-specific question what I really wanted to avoid. So, let me do some additions: When I say interface, I don't mean t

  • Do I need static libraries to statically link? 2009-06-20

    On 'C', Linux, Do I need static libraries to statically link, or the shared ones I have suffice? If not, why not? (Don't they contain the same data?) --------------Solutions------------- Yes, you need static libraries to build a statically linked exe

  • Static vs. non-static method 2009-07-26

    Suppose you have some method that could be made static, inside a non-static class. For example: private double power(double a, double b) { return (Math.Pow(a, b)); } Do you see any benefit from changing the method signature into static? In the exampl

  • Static vs. non-static method 2 2009-07-27

    Possible Duplicate: Static vs. non-static method which one is better for a good design or are there any difference? or is it just up to the developer? class Foo { int x; void add(Foo* f1) //Method 1 { x += f1->x; } static void add(Foo* f1, Foo* 2)

  • Java: Static vs non static inner class 2009-08-30

    This question already has an answer here: Java inner class and static nested class 16 answers What is the difference between static and non static inner class? --------------Solutions------------- An inner class cannot be static, so I am going to rec

  • distinguishing between static and non-static methods in c++ at compile time? 2009-09-17

    For some tracing automation for identifying instances i want to call either: a non-static method of the containing object returning its identifier something else which always returns the same id My current solution is to have a base class with a meth

  • C# - Get values of static properties from static class 2009-09-28

    I'm trying to loop through some static properties in a simple static class in order to populate a combo box with their values, but am having difficulties. Here is the simple class: public static MyStaticClass() { public static string property1 = "Num

  • Initialization Order of Static Fields in Static Class 2009-09-29

    given the following code: public static class Helpers { private static Char[] myChars = new Char[] {'a', 'b'}; private static Int32 myCharsSize = myChars.Length; } Is it guaranteed that myChars will be initialized before I use its length to assign to

  • Should a class ever have static and non-static members 2010-01-29

    I'm trying to figure out when it would be appropriate for a class to have both static and non-static functions. AKA: $obj = new ClassA; $obj->doOOPStuff(); $something = ClassA::doStaticStuff(); Note: This example is done in PHP, however the questi

  • static vs non-static method for immutable class 2010-02-01

    Given the class definition below. How would one go about deciding whether the stub methods should be static or non-static? class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } // Should the me

  • Objective C: Initalizing static variable with static method call 2010-05-11

    The Compiler claims an error saying: "initializer element is not constant", when I try to initialize a static variable inside a method with a call to a static method (with + in its definition). Anyway I can tell him that this method always returns th

  • trouble accessing non-static functions from static functions in AS3 2010-06-05

    I have a class containing, among other things, a drop down menu. With the aim of saving space, and since the contents of the menu will never change, I've made a static DataProvider for the whole class that populates each instances menu. I was hoping

  • A way to get at non-static information from Static Context 2010-06-30

    I know you can't static members from instance members. But lets say I have in one assembly this: public class ClassA { public List<order> GetOrders(int orderID) { ... } } Then in another assembly this: public static ClassB { private static void

  • C# comparing private static and public static method 2010-12-10

    In C#, what is the difference between methods that are markedpublic staticand methods marked asprivate static? How are they allocated and accessed? --------------Solutions------------- A private static method can only be accessed within the class tha

  • synchronized blocks for static and non-static methods 2010-12-11

    I created two threads and using a single instance of class called the static and non-static methods of that object. Ideally static methods need to be called using the class name and i did that too. I synchronized both the static and non-static method

  • What is the difference between static methods in a Non static class and static methods in a static class? 2011-03-09

    I have two classes Class A and ClassB: static class ClassA { static string SomeMethod() { return "I am a Static Method"; } } class ClassB { static string SomeMethod() { return "I am a Static Method"; } } I want to know what is the difference between

  • Are classes in Java `static` or `non-static`? 2011-03-13

    Are classes in Java static or non-static? --------------Solutions------------- Static applies only to blocks, methods and class member variables. There's no meaning in having a Class Static, although an inner class can be static member of its enclosi

  • detecting the static objects in static background 2011-04-15

    Problem statement: How can we identify the static objects in static background based on pixel intensity distribution? that mean extract the background and foreground from a static image or picture.? How could is this possible? Please give me your vie

Copyright (C), All Rights Reserved.

processed in 0.496 (s). 13 q(s)