wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Static Variables and Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Variables and Methods" Watch "Static Variables and Methods" New topic
Author

Static Variables and Methods

Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
Hello,
I have question regarding static variables and methods. we all know static variables and methods accessed by ClassName.StaticMethod
or ClassName.StaticVariable. But, I want to know that In Programs or any application, In which situation, we should make any method or variable as static. and why ?

Thanks
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Any method or variable that is independent of the state of an instance of the class should be static.

Oh, and you might like to reply to the two responses on your previous thread.


luck, db
There are no new questions, but there may be new answers.
Fyodor Sherstobitov
Greenhorn

Joined: Feb 21, 2011
Posts: 9
Static methods and variables belong to the class, not the instance. This means that you can access it with no need to create an object of class using ClassName.staticVariable of ClassName.staticMethod() constructions.
Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
I know that to call static method or static variables we don't need to create any object. we can call them with Class Name. but, if I develop any application or project, some situation occurs when i need to call static method or static variable i use. but, what are those situation. can you give me an example ? so, i can get better idea ....
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Rishabh Shah wrote:can you give me an example ? so, i can get better idea ....

A variable which maintains the number of times a page has been viewed. In this case you have to use a static variable. If you use an instance variable here you will not get the correct count.
muralidhar nayani
Greenhorn

Joined: Aug 05, 2011
Posts: 2

The use of static variables in real time projects are : Ticket Reservatio System in Railways or Busstations. The passenger ticket number is static it is going to increase with the perticular serial number.

In this scenario also the static variables we can use.

If the static variable value can not reinitialize.

for more visit: http://javabynataraj.blogspot.com


for more visit: http://javabynataraj.blogspot.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38515
    
  23
Welcome to the Ranch muralidhar nayani and sagar gurjar, both of you.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Just to add some examples... static methods & variables are useful in counter... and in the singleton pattern..

below the id counter is static variable...


below getInstance() is a static method that enables singleton pattern


Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
Thank you so much everyone...I got an idea now..Thanks a Lot !
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Rishabh Shah wrote:Thank you so much everyone...I got an idea now..Thanks a Lot !

You are welcome Rishabh
Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
its been written that
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
Singleton s3 = Singleton.getInstance();

it means that the object is created once only, but, it is reference by s1,s2,s3....is it so ?


Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
Rishabh Shah wrote:its been written that
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
Singleton s3 = Singleton.getInstance();

it means that the object is created once only, but, it is reference by s1,s2,s3....is it so ?


bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Rishabh Shah wrote:its been written that
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
Singleton s3 = Singleton.getInstance();
it means that the object is created once only, but, it is reference by s1,s2,s3....is it so ?


Yes. For more information please read Singleton Pattern
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38515
    
  23
Agree, but beware: many people nowadays think singletons are not a good design pattern; indeed some people say "singleton antipattern".
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Campbell Ritchie wrote:Agree, but beware: many people nowadays think singletons are not a good design pattern; indeed some people say "singleton antipattern".

Campbell, Could you please explain this in detail?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38515
    
  23
No. But try here, and search the Ranch for similar posts.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Static Variables and Methods