Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods Vs "Instance Methods"

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When do i use static methods ? One is when i am not modifying the "state" of an object. Is there any other ?
 
Raj Bhunia
Greenhorn
Posts: 8
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods are useful for creating utility classes.Because utility classes are not meant to be instanciated.Like java.lang.Math class.

Static methods are also useful for creating singletons , factory methods.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Bhunia wrote: singletons


What are they and what are their uses ?
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singleton pattern
 
Raj Bhunia
Greenhorn
Posts: 8
Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singleton pattern is a popular design pattern which proposes that only one instance of a class will be created by JVM.Repeated calls always return the same instance.When programmers need to be concerned about meaningful use of resource then use this pattern.For example ,if programmers create a new connection object every time it requires , then clearly it is a wastage of resource.In this case we can create a singleton connection class and that single connection object will be used throughout the application.



If you need connection object just use it like this....


Another example where singleton is used frequently is in hibernate when you instanciate SessionFactory.Because SessionFactory is very resource intensive.So instanciate it only once.


I hope this will help you.For more info go to this link Singleton Pattern
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please give me a brief explanation of how the database program is using one instance to save memory. Also, please put the code in tags for ease of reading.
I cannot use the hibernate example because i have not yet learned that.

thanks
rb

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be cases where you will have utility methods which do not depend on the availability of the instance but are methods related to the class- like obtaining the count of the instances created so far.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:There might be cases where you will have utility methods which do not depend on the availability of the instance but are methods related to the class- like obtaining the count of the instances created so far.


I am not clear about utility methods.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:
I am not clear about utility methods.

Suppose you have a class- say a class mapped to a certain table in the database- we would call it as a model class. And you want to add a method to retrieve all the rows which satisfy a certain criteria. As this doesnt require an instance to be created, we mark this method as static.
 
Nico Van Brandt
Ranch Hand
Posts: 70
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Utility methods are methods that have nothing to see with the actual business logic of the application.
They only exist to 'help' the programmer. Because they don't use class fields, they can be left static.

Example (found on the web);
 
Raj Bhunia
Greenhorn
Posts: 8
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:
how the database program is using one instance to save memory


Lets test that database code......


and the output of that code is : Same reference......

The reason is when ever the ConnectionUtil class is loaded into the memory that static block is initialized and connection object is created.How many objects you create it doesn't matter , same reference will be given to every object .
 
Nico Van Brandt
Ranch Hand
Posts: 70
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other uses of static fields:

Initialize a logger
Maybe you want to write a log line in the constructor, there fore you need to initialize it.

Couning
Having some sort of counter where the value is the same for all instances of the class.

Example combining the two:


 
Raj Bhunia
Greenhorn
Posts: 8
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:
I am not clear about utility methods.

Imagine you have a class that have a method and it always run in the same way , it's sole purpose is to return some thing ,doesn't matter which instance of that class perform that method.These type of methods are called utility methods.Utility methods are static methods.For example ,Math class has several utility methods like random(),exp(double),log(double)
etc.We don't need create an instance of Math class( you can't create an instance of Math class because it has private constructor).Use these methods by using class name only.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic