• Post Reply Bookmark Topic Watch Topic
  • New Topic

static methods in a class  RSS feed

 
AbhiRam Kalyan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody tell what will happen if i declare all the methods in a class as static ?

Thanks in advance...
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will be able to access them without instantiating the object (i.e. with the new keyword)

If all the methods are static, you will not be able to use them to alter non-static information in an instantiated object if you DO instantiate the object.

You will defeat the idea of object oriented programming because the objects you create will not have individual behaviors.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you should give that class a private constructor which you never call, so you never create any objects. Look at the java.lang.Math class, which does exactly that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!