-1

Trying to come up with a good strategy to solve my problem. The problem is I have a final class that contain private constructor and static methods. In order to use any of the static methods I have to initialize an instance of the class. Because I have data in the constructor that the methods need in order to function.

So, why am I making the methods to static? Because 1) the project is so large that i need for example MyClass.AccessThis() and 2) is easier than create an instance and call the method.

EDIT: What i mean with 2) is if i turn all my methods to non-static, public constructor then i can create an instance of the class in any class and use my methods.

EDIT 2: So here it goes.

public final class StackOverFlow {

private static Map<String, String> map = new HashMap<String, String>();

    private StackOverFlow() {

        map.put("hello", "hello");
    }

    public static String getHello()    {
        return map.get("hello");
    }
}

I can't obviously get the data if i call the method from an another class by StackOverFlow.getHello(); because the data dont exist.

I am wondering is there any good way to get the data without putting the data in the static method?

bCM
  • 97
  • 1
  • 3
  • 12
  • 2
    Please post your code. – TimoStaudinger May 17 '15 at 22:15
  • 1
    *Because I have data in the constructor that the [static] methods need* - how do you intend to access that instance data from a *static* method? Your question is not very clear because it seems to be based on an impossible premise. – Greg Hewgill May 17 '15 at 22:16
  • 3
    I'm pretty sure you're doing object-oriented programming wrong... :D – Jose Salvatierra May 17 '15 at 22:16
  • Static methods are not related to an instance. So you can place the data from the constructor to a static field.. Or use a singleton. – Bubletan May 17 '15 at 22:16
  • So what's your question? (The only question in your text - the sentence with a quesiton mark at its end - is answered by yourself.) – isnot2bad May 17 '15 at 22:19
  • Bubletan, thanks! I will look at Singleton design pattern. – bCM May 17 '15 at 22:20
  • You don't need an instance to call `static` methods. What made you think that you do? – Elliott Frisch May 17 '15 at 22:20
  • While your at it I would read in the other design patterns, because if you want to use singleton it is bad design in like 99% of the cases. – maraca May 17 '15 at 22:28
  • Read the second Edit – bCM May 17 '15 at 22:29
  • @maraca: "... if you want to use singleton it is bad design in like 99% of the cases.": Hyperbole much? – scottb May 17 '15 at 22:31
  • @scottb You mean I exaggerate? http://stackoverflow.com/questions/12755539/why-is-singleton-considered-an-anti-pattern http://stackoverflow.com/questions/11292109/why-implementing-a-singleton-pattern-in-java-code-is-sometimes-considered-an-a http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons (last link is probably the best) – maraca May 17 '15 at 22:50
  • @maraca: Of course, there really are things that are physically represented as just one single thing (eg. a file system). I would say that you are mistaking an "anti-pattern" for "an overused pattern." – scottb May 17 '15 at 22:56
  • @scottb yes, I didn't wanna upset you, I said 99% not 100%, you can find Singletons in the native Java libraries, but very often the single motivation is "because it is easy, everything is global and can be accessed from anywhere like if I put it all in the main function", when instead they just could have created a class with static methods. – maraca May 17 '15 at 23:03
  • I would say that a 99% misuse rate (a use that violates a solid foundational principle) is an anti-pattern. 99/100 uses of a "constant interface" for example. As there are valid uses for singletons, the suggestion that 99% of singletons are misused must be hyperbole. – scottb May 18 '15 at 05:57

3 Answers3

1

In order to use any of the static methods I have to initialize an instance of the class. Because I have data in the constructor that the methods need in order to function.

No. This is not correct. This is not how static methods work.

For every class you write, there are two possible data structures:

  • Class object. Every class has an associated Class object.
  • Instances. Classes may optionally also have instances (defined by the class)

Static methods and fields are associated with the Class object. They are independent of any instance. Static members are therefore unable to access any data that is part of an instance. Conversely, any instance may access any of the static members that belong to the Class the object.

You can absolutely use all of the accessible static methods that are defined in your class without having an initalized instance of it (because static members belong to the Class object and not to any instance).

I can't obviously get the data if i call the method from an another class by StackOverFlow.getHello();

I am wondering is there any good way to get the data without putting the data in the static method?

Oh yes you can. The data belongs to the Class object, StackOverflow. Any object that can access your StackOverflow Class and has access to its methods can get your data, no matter where it is in your project. Static fields are a kind of global data. In your case, any method that is in the same .class file as StackOverflow can get to your map object.

Of course there are alternate methods. It is up to you to come up with a good data model for your project. You could, for example, have your data in an instance of a singleton object whose reference you pass to other methods.

scottb
  • 9,908
  • 3
  • 40
  • 56
0

If you create a static method you can call it without initializing it. for example:

class MyClass {
    public static int myMethod() { ... }
}

The call should be :

int result = MyClass.myMethod();
isnot2bad
  • 24,105
  • 2
  • 29
  • 50
Dev pro
  • 620
  • 1
  • 7
  • 12
0

This might be what you are looking for, static initialization:

public final class StackOverFlow {

    // remove the final if you have methods modifying the map
    private static final Map<String, String> map = new HashMap<String, String>();
    static {
        map.put("hello", "hello");
    }

    public static String getHello() {
        return map.get("hello");
    }

}

Like that you never need to create an object to access getHello().

maraca
  • 8,468
  • 3
  • 23
  • 45