0

Let's say i have a class, and I made only one instance of it and i don't need more than that.
Should i just make the class static ? (not the class itself but the functions and the variables).

In the example below should i make the class static if i won't make more than one instance of it ?

public class Foo {
    int num1;
    int num2;

    public void func() {
        // Something in here
    }
}

public static void main(String[] args) {
    Foo bar = new Foo(); //I don't need more than one instance of that class.
}
julian
  • 4,634
  • 10
  • 42
  • 59
  • it mean singleton class – Kick Feb 03 '14 at 18:15
  • Look up 'singleton' http://en.wikipedia.org/wiki/Singleton_pattern – Rob Audenaerde Feb 03 '14 at 18:15
  • 5
    While there is such a thing as a static *inner* class (which is not what you need), there are no stand-alone static classes with Java. And you're better off avoiding use of singleton here as its use can be full of traps if you're not careful. Why not simply create your one instance and use it? – Hovercraft Full Of Eels Feb 03 '14 at 18:15
  • You can have a class with no instances, and only static methods (and you can hide the constructors so no instances can be created). Or you can have a class and only create one instance. You can also create a so-called "singleton". But beware -- singletons are very seductive and easily abused. – Hot Licks Feb 03 '14 at 18:19

7 Answers7

2

You can use an enum to define a singleton.

public enum Foo {
    INSTANCE;

    int num1;
    int num2;

    public void func() {
        // Something in here
    }
}

public static void main(String[] args) {
    Foo bar = Foo.INSTANCE;
}

However, this is only need if you want to enforce one instance. Otherwise, I would just use new Foo(); and call it only once, if you only need one.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2
  • If your class has no state, say:

    class NoState {
        static int sum(int i1, int i2) { return i1 + i2; }
    }
    

    then it makes sense to use static methods.

  • If you must ensure that there is only one instance of your class, then you could use a singleton, but be careful: global state can be evil.

  • Not as bad as a singleton, you could use static fields/methods: it can be useful is some situations but should not be abused.

  • In any other situations (= most of the time), just use normal instance variables/methods.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I liked the to the point answer. I accepted your answer because you wrote what to do in many cases. so i learned a lot from your answer, Thank you for that! – julian Feb 03 '14 at 19:11
  • One probably should declare `sum` to be `public`. – Hot Licks Feb 04 '14 at 17:53
1

You can use Singleton. However, make sure if Singleton is what is really required - sometimes singletons gets overused where simple class with static methods might suffice. There are many ways to create singleton as explained What is an efficient way to implement a singleton pattern in Java?

Note that with Java 5, enum is the preferred way to create singleton.

Community
  • 1
  • 1
user1339772
  • 783
  • 5
  • 19
1

You say that i don't need more than that so my answer is that not make more than one and if you really like to enforce the instance that it should be only one for class then use the enum best way to implement the singleton in java

for example in datasource one really needs singleton

public enum UserActivity {
    INSTANCE;

    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;

    private UserActivity() {
        this.dataSource = MysqlDb.getInstance().getDataSource();
        this.jdbcTemplate = new JdbcTemplate(this.dataSource);
    }

    public void dostuff() {
     ...
    }
}

and if you really need that then use it otherwise go with your current logic

Girish
  • 1,717
  • 1
  • 18
  • 30
0

A class that must be instantiated once and only once, is called a singleton. That knowledge should help you narrow down your search for information. To give you a head start:

Community
  • 1
  • 1
Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45
0

Basically static methods and fields means that you don't need any instances of the class.

In you case you need 'singleton' class, you can either use enum or make it a singleton by yourself, using the specific set of rules.

XpressOneUp
  • 195
  • 10
0

It really depends on the scope of your application. If you want this object to be used as a singleton you would provide a static method to get the one and only instance of the class.

public class Foo
{

     private static Foo instance ....
     private Foo()
     { 
         ..... 
      }

      public static Foo getInstance()
      {
         return instance;
      }

}

If you plan to use a framework like spring you would just add one object to the application context.

<bean class="....Foo" id="fooInstance" scope="singleton">
  ....
</bean>

But maybe, you can refractor this class to hold only static methods. Then you can mark the class as final and provide a private constructor.

public final class Utils
{
     private Utils(){}

     public static .... doFoo(....)
     {
           ....
     }
}
Hannes
  • 2,018
  • 25
  • 32