Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Variables and Methods

 
Rishabh Shah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5149
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fyodor Sherstobitov
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
Chrome Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 51436
87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch muralidhar nayani and sagar gurjar, both of you.
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much everyone...I got an idea now..Thanks a Lot !
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rishabh Shah wrote:Thank you so much everyone...I got an idea now..Thanks a Lot !

You are welcome Rishabh
 
Rishabh Shah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51436
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, but beware: many people nowadays think singletons are not a good design pattern; indeed some people say "singleton antipattern".
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51436
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. But try here, and search the Ranch for similar posts.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic