*
The moose likes Beginning Java and the fly likes Static methods Vs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static methods Vs "Instance Methods" " Watch "Static methods Vs "Instance Methods" " New topic
Author

Static methods Vs "Instance Methods"

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

When do i use static methods ? One is when i am not modifying the "state" of an object. Is there any other ?


SCJP 6. Learning more now.
Raj Bhunia
Greenhorn

Joined: Apr 10, 2011
Posts: 8

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

Joined: Jan 21, 2011
Posts: 637

Raj Bhunia wrote: singletons


What are they and what are their uses ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Singleton pattern


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raj Bhunia
Greenhorn

Joined: Apr 10, 2011
Posts: 8

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

Joined: Jan 21, 2011
Posts: 637

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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.


Mohamed Sanaulla | My Blog
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Mar 31, 2011
Posts: 66

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);


Oracle Java SE6 Certified Programmer
Oracle Java EE5 Certified Web Component Developer
Raj Bhunia
Greenhorn

Joined: Apr 10, 2011
Posts: 8

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

Joined: Mar 31, 2011
Posts: 66

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

Joined: Apr 10, 2011
Posts: 8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods Vs "Instance Methods"
 
Similar Threads
Static Methods and Instance Methods
Stoping a Thread when is not needed anymore
why do we get 40?
Static methods
Static binding question