• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static methods Vs "Instance Methods"

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 8
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Bhunia wrote: singletons



What are they and what are their uses ?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singleton pattern
 
Raj Bhunia
Greenhorn
Posts: 8
Netbeans IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic