A class with only static methods?

If I have a class with only static methods, that are just changing values of instance variables and returning some values to calling methods, is there a need for a constructor? Will one ever be called because I will never create an object of this class and will only call these methods to retrieve values needed elsewhere. In addition, if the methods are all static, does the class need to be declared in any special way? Also, how can I reset all of the instance feilds of this class? Can I just write another static method that will set the instance variables to the desired values?

Or am i going about this the wrong way? I am very new to OOP.

[659 byte] By [woohoo1a] at [2007-10-2 14:00:56]
# 1

> that are just changing values of instance variables

> and returning some values to calling methods,

To me, this smells wrong. Maybe a Singleton, or just a standard object.

> is there a need for a constructor?

In the design you have outlined (its not OOM, but what the hell), I would have a no-arg private ctor, so no one could created an instance of the none-object.

> Will one ever be called because I will never create an object of this class

No, if you don't create an object, a ctor is not called, but someone else could (as Java will add a public no-arg ctor, unless you have a ctor).

> In addition, if the methods are all static, does the

> class need to be declared in any special way?

No

> Also, how can I reset all of the instance feilds of this class?

As I said this smells wrong, but without knowing what you are up do, I can't say what to change it with. But yes, from how you have described, a "reset" method would be fine.

> Or am i going about this the wrong way? I am very new to OOP.

I'd say yes, but without knowing more I can not give an alternative.

mlka at 2007-7-13 12:07:43 > top of Java-index,Java Essentials,Java Programming...
# 2

If your class only has static methods, there is no need for instance

variables, because there is no use for them cf. the System class.

I you want to protect your class from having any instance being created

implement a private default constructor:public YourClassWithStaticMethods {

private YourClassWithStaticMethods() { }

...

}

With this private ctor, explicitly implemented by you, the compiler is not

allowed to implement one for you. The class can also not be extended

because of this private ctor.

kind regards,

Jos

JosAHa at 2007-7-13 12:07:43 > top of Java-index,Java Essentials,Java Programming...
# 3

> returning some values to calling methods, is there a

> need for a constructor? Will one ever be called

Not a NEED. But to really be correct you should make a constructor like

private MyClass() {}

which will make it totally uninstantiatable.

> all static, does the class need to be declared in any

> special way? Also, how can I reset all of the

Nope. You don't need a special declaration although you might want to add some javadoc comments

> instance feilds of this class? Can I just write

Do you mean instance fields or static fields? Hopefully static fields as you'll never have an instance and thus no instance fields

> another static method that will set the instance

> variables to the desired values?

Sure you could do this

tjacobs01a at 2007-7-13 12:07:43 > top of Java-index,Java Essentials,Java Programming...
# 4
Well I think the use of all static classes are not bad design if you use it properly. It usually good for utilities and Factory classes, For example Math, Collections, BorderFactory etc
WirajRa at 2007-7-13 12:07:43 > top of Java-index,Java Essentials,Java Programming...
# 5

> Well I think the use of all static classes are not

> bad design if you use it properly. It usually good

> for utilities and Factory classes, For example Math,

> Collections, BorderFactory etc

Yes, all static methods are fine in the there place. But normally they don't have variables.

mlka at 2007-7-13 12:07:43 > top of Java-index,Java Essentials,Java Programming...