I’ve heard some talk about static classes, but can’t find any clear info on them. Are they classes where everything is by default static instead of dynamic? And I heard that they’re going to be added in 1.5. Is this correct?
Thanks.
I’ve heard some talk about static classes, but can’t find any clear info on them. Are they classes where everything is by default static instead of dynamic? And I heard that they’re going to be added in 1.5. Is this correct?
Thanks.
IMHO ‘static classes’ are classes where you made everything static and don’t have a ctor.
Concerning 1.5, I only have in mind the static imports, that allow a slightly more elegant usage of constants. But I feel this will be a minor detail, no big deal.
Eww…
never use static classes.
Always use singletons
[quote] never use static classes.
Always use singletons
[/quote]
???
Why in [insert favorite worhiped entity(ies)] name(s) would you want to give that advise?
static is a perfectly valid keyword, and can be used quite powerfully. Use what suits you best!
[quote]IMHO ‘static classes’ are classes where you made everything static and don’t have a ctor.
[/quote]
All classes have a constructor, though it might now have a public one… so if it is private, it can still be instantiated by itself. Other than that, I agree with Herkules.
FYI:
nickdotjava - you have heard about static imports, not static classes. Static classes are inside java since 1.2, it is static imports that get introduced inside 1.5.
Static imports - ability to put static methods and fields of given class into ‘default’ namespace of class you are using. When you static-import Math class, you can later write cos(PI) instead of Math.cos(Math.PI). This is just a syntax sugar, compiler-only, has not effect on bytecode/execution.
Static class - this is a special case of inner class. Normally, inner class has invisible pointer to enclosing class, which allows it to access enclosing class members. But sometimes, you just want to have small utility class as inner class, without burdening it with extra pointer inside. Example (althrough not best, because it inherits from enclosing class) is Rectangle2D.Float and Rectangle2D.Double.
nech_neb - you probably meant ‘never use static methods, use singletons’. I have heard it before few times. While it is a valid religious belief, which can be even supported by heap of anecdotal evidence, remember that you will also need to bend sometimes - for example, to retrieve singleton, you need static method Same is true for many utility methods - there is really no need to make java.lang.Math class singleton.
I think nech_neb really did mean don’t use static classes.
While I agree in general that Singletons are better than static classes, there can be times when it makes sense to use an actual static class – java.lang.Math being a good example. But a static class should ideally be used only when it doesn’t make any sense to use a Singleton – i.e. when there are no data members, when none of the methods will ever be used outside of a static context, etc.
And I’m sure someone is going to chime in with: “But it’s different for games!”
I personally really LOVE statics for they remember me of god old times, when programming was easy…
Than came the time when everything had to be pure OO, everything needed to be an object - darn, that was hard! Now I’m half way back again and I feel better now.
private final static - that’s my favorite modifier list
[quote]I think nech_neb really did mean don’t use static classes.
[/quote]
There is a difference between ‘class with static methods’ and ‘static class’. Latter has very well defined meaning in java and has nothing to do with former. I suppose that by ‘static class’ he referred to ‘class with static methods’, because it is a construct which can be replaced with Singleton. ‘Static class’ in java meaning can be replaced with top-level class in same package, but has nothing to do with singletons.
Sorry to be picky about the words, but IMHO vocabulary common to people discussing is neccesary if you try to explain any ideas.
For info about ‘static class’ (called ‘static nested class’)
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#262890
[quote]I personally really LOVE statics for they remember me of god old times, when programming was easy…
[/quote]
Back when all variables were global, with none of this pesky “data hiding” and “encapsulation” and stuff?
abies: Thanks for the clarification!
Use singletons where you have to. When you start doing static classes you are already out of “pure” oo since you are basically creating a global.
Also using singletons in java is kind of stupid since you can’t pass by reference and is analogous to argument use goto instead of while in loops.
Why would anyone use static class instead of singleton class where singleton pattern is appropriate. Static classis ugly, results in odd behaviour and breaks all the class hierarchies you might ever have.
Static class = design flaw and there is no other way around it.
Also my post contains a paradox, take that as you wish
Static classes aren’t global, and nor are they flawed… they do exactly what they say they do, and if the situation fits, then it’s the right thing to use.
Cas
In java they are “global” by nature because you can access them anywhere. This is in static class where there are only static members and functions, for example Camera.x where x is static int.
Howeve when using static Camera camera = new Camera(0, 0, 0); you have to be careful whether you are supposed to use singleton.
Not correct, depends on class and method visibility. I.e. they “belong” to their class and are not “global”.
Why?! As any static method is accessed by Camera. (notice Case) it’s obvious that there is nothing “special” about instantiating an object of a class with static methods (just that if the class contains only static methods it’s rather bad design though…).
(And don’t come with the lame excuse that camera. also works - this is an error even though you only get a compiler warning - this should be fixed a.s.a.p.)
Because you can’t do a proper singleton design in java. And it is not a lame excuse. You are violating at least 2 major OOP principles (information hiding, encapsulaing single access). That is why. Let’s take an example here: http://c2.com/cgi/wiki?JavaSingleton
Singleton is supposed to be a fast and efficient way to go around things, not a gimmick hack. When implementing a singleton in java, you are almost forced to use static class one way or another, thats why the term java singleton.
This is one of the downfalls in java, because no better way has been implemented yet. How come can you access Camera. if you have to init the class anyway or pass it around camera and use camera.? Thats a major design flaw. If you have a constructor it will also allow constructing the class(“heap” or “stack”) anywhere and you can simply access things this way.
///Hopefully the programmer has non static methods withing the camera
Camera camera;
camera.doStuff();
//Or static methods only
Camera.doSuff();
This is the reason if you are going to create static Camera camera you should not have static funtions inside the camera. The former one especially is bad design and even worse if both can be accessed at the same time.
I would be very careful when playing around with static code especially if someone else has to use it.
Besides when you are saying they belong to the class and thus not global, you are wrong. You are just moving the dot around the problem and making it seem better for your own purposes. A global is a global no matter where it is if you can access it anywhere. It doesn’t make difference whether it is in a class or not. You are violating class hierarchies.
static NoGlobalsHere omgNoGlobals
for(int i =NoGlobalsHERE.lol; i > x; i–){
omgNoGlobals.globarlRBad(lol);
}
Either way you are doing a global access.
If you are tight about design do it right especially in Java because it is one of the few languages that provides you with really tight quidelines already,
There is nothing wrong with static classes. What is wrong is trying to follow some sort of rule book to keep everything “purely OO” when that is just plain silly. Do what fits your problem. OO programming is not a requirement. It is simply a method of doing things that is usually helpful.
I have seen people waste so much time trying to figure out the “right” Oo way to do something, when they could just simply write code that gets the job done. They are stuck thinking that somehow this code will be cursed and cause tons of problems when for all practical purposes that is rarely the case, and if some issue is found later on a slight refactoring could overcome it. Don’t over design trivial operations, just be aware of the larger picture.
Sure some practices can lead to more errors than others, but there is no hard rule that any particular practice will always cause a problem.
Everyone seems to have forgotten that classes are local to a classloader as well so it’s quite possible to have multiple instances or purely static classes.
Cas
Yes, you can - and many have.
Yes it is ;D
[quote]You are violating at least 2 major OOP principles (information hiding, encapsulaing single access). That is why. Let’s take an example here: http://c2.com/cgi/wiki?JavaSingleton
[/quote]
information hiding:
declare class
package com.my.singleton.whatever;
public class Whatever {
/* package */ Whatever (...) { ... }
public void doSomething(Object data);
}
encapsulating single access:
provide factory
package com.my.singleton.whatever;
public class WhateverFactory {
private static final Whatever whatever = ...;
public static Whatever getMyWhateverSingleton() { return whatever }
}
Any questions so far?
(and this is just an example mind you - use whatever works…)
Not always - it can also be a design feature.
Please enlighten me on what you consider a static class?
This above make no sense to me - what are you trying to say?
wrong
ehh?
ehh??
Why would you feel any different when using static methods versus non-static methods?
Tips:
try to declare a class with no access modifier (i.e. package only) and access a static method of that class from another package - maybe you’ll see the light
I agree. But since that aint the case in java I don’t understand why you keep rambling about it?
If that above is supposed to tell me something the only thing I can think of is that I’m very happy that I usually program in java and only touch c/c++ when I have to.
I agree(?)!
[quote]Everyone seems to have forgotten that classes are local to a classloader as well so it’s quite possible to have multiple instances or purely static classes.
Cas
[/quote]
IIRC:
Nitpick: they aren’t consider the same class though if they are loaded by different class loaders ;D
EDIT: Maybe that was your point, silly me :-[
1.) It is not a singleton. You pass singleton as around as a reference or a pointer. This does not work in java, because you have to duplicate the class. Dupe != singleton. Read “Design Patterns” p149, it’ll explain you why “Java singleton” is a gimmick (not even a hack because you can’t have single access to a class everywhere.
2.) Globals: A class that has members accessible any class without construction can easily be considered global class. Globals have few uses, mainly when working with hardware, but usually then it is a design flaw. If you take into account what princec said, your singleton is not a singleton. If you can show me a way to implement a true singleton in java I’ll let you have my manbabies.
This is considered a global in java. Now you can do all smarty things like information hiding and stuff, but it is still a global if you access it Global.fag instead through the constructed object.
public class Global{
public static fag;
publiv static hat;
}
now you can access anything Global.fag ot Global.hat.
2.1) You should use static member functions only when you are handling static member, for example returning it. Other wise you’ll get big cross overs in class hierarchies again.
Concerning tips: No I don’t see the light. That is just plain bad design and I hope your co-workers appreciate your insightful addition to the world of design.
Personally, I’d never use singleton, because there are so many oppoturnities to miss when using a singleton and when you miss, other people who read your code can’t understand it and your classes will be worthless considering future usage…
If you don’t use class hierarchies and proper encapsulation your code will be only good for yourself.
Only way I can think of having a singleton in java is using the VM namespace and declaring a class with static methods only that never gets constructed. However this again imposes some problems and is bad design.
It feels more and more like you don’t even read what I write and are on some kind of strange religious battle trying to explain that java is evil (even though admittedly you do it by making claims that are at best misleading and in some cases even right out false)?! :
Well, one more reply trying to explain my views on your claims comes here…
What is not a singleton?
[quote]You pass singleton as around as a reference or a pointer.
[/quote]
If you like too, I see no problem with that yes (even though I don’t use singletons much- when I do, I usually don’t “pass them around”)
Here you’re making a claim that I don’t understand - please explain. Duplicate the class!?
[quote]Read “Design Patterns” p149, it’ll explain you why “Java singleton” is a gimmick
[/quote]
I don’t have a copy of that book here (vaccation).
[quote] (not even a hack because you can’t have single access to a class everywhere.
[/quote]
In reality no (as you can have multiple java vm’s running)
But then you could argue that you can’t have any singleton whatsoever (another OS running, another machine running ad absurdum…)
Not a java class - unless our definition on global is different. My is something like this:
Global:
A java class is clearly not a global according to neither of those two requirements.
I agree that usually globals are a design flaw.
If you take it ad absurdum you can never have a singleton…
And I showed you a pretty good singleton above
even though it obviously didn’t fit your belief in the mighty über-singletons religion
</flamesuite off>
Anyway: but as I said: If the same class (the “singleton”) is loaded by two different classloaders you can’t use both of them as the same singleton (i.e. they are both singletons - but different - strange, yes, but then so is most of computer science theory) - you will get a ClassCastException (IIRC) as they are no longer the same singleton / they are different.
Well, then - if you could provide your defintion of globals maybe we could call that
Captain-Goatse Globals, or short form CGG
[quote]Now you can do all smarty things like information hiding and stuff, but it is still a global if you access it Global.fag instead through the constructed object.
public class Global{
public static fag;
publiv static hat;
}
now you can access anything Global.fag ot Global.hat.
[/quote]
Above makes no sense, first of all I guess you mean that Global is just a class with two public static Singleton classes member variables (case is misleading though)?
Not sure what this got to do with globals, except that it is providing a good lesson in bad design.
Now your getting religious again :), my oppinion is that use what works - and static methods are a great way to provide utility methods (Math.sin etc etc)
Well, the point was that you can have different visibillity on java classes - thus breaking one of the fundamental properties of a “global”.
[quote]That is just plain bad design and I hope your co-workers appreciate your insightful addition to the world of design.
[/quote]
touché, well I don’t have any problems with constructive critique ;D
Singletons have their uses.
Tthere are probably better designs available when you use one - but then, who cares, if it works, it’s documented and not totally confusing?
Nope, that’s religous OO talk - there are many ways to design, OO is just one of them.
VM namespace? You lost me there