"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
where should we must use static and where static methods should not be used?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Stan James:
They are never really necessary. You could always do new Math().method() instead of Math.method().
There are some disadvantages. When you call one, you couple yourself directly to the class. It's impossible to use polymorphism and swap in another class later.
I've used them as a lazy shortcut (to avoid having to instantiate and object) and come to regret it several times. I stop and think hard before I add new static methods and especially classes with all static methods.
"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Originally posted by David McCombs:
... You can instantiate Math?
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by krishna balaji:
i understood concept of static variables
can any one explain concept of static methods ?
why do we use static methods?
what all situations in which we can use static methods ?
why main is static?
where should we must use static and where satic methods should not be used?
Thanks
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Gavin Tranter:
Dont you need static methods, when you wish to create objects using the Singleton pattern?
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Originally posted by Roger Chung-Wee:
Another thing to consider is that you cannot override a static method because it is implicitly final.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Originally posted by Roger Chung-Wee:
....This makes JUnit testing difficult, so I would only use a static method which performs some small utility function and which I won't be JUnit testing.
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Don't get me started about those stupid light bulbs. |