• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

What is the use of STATIC variable, calss, method??

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends
I need a help from you all.
I want to know about the usage of STATIC keyword in a class/method/variable...

Memory allocation is one advantage for STATIC keyword...
any more advateags with STATIC???


Please help me
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables for instance are also called 'class variables'. They only exist once per class, not per instance.
That makes static members instance independent, they share the same value among all instances of your class.

A good example for static methods are utility methods such as those in the Arrays or Collections class.
To perform Collections.emptyList() you do not need an instance because this method does not require any state. Hence it is a good decision to mark it static.
 
Marshal
Posts: 72406
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Agree with Sebastian Janisch. About methods: if a method takes no information from the state of an object (it uses no instance fields) and it imparts no information to the state of an object (it alters or manipulates no instance fields) then you should consider making it static

Note that many books use the main method a lot, because it is quicker to put a little code example in the main method than to write proper object-oriented code. So they have examples where all their methods are marked "static" because you cannot access instance fields, instance members, the keyword "this" and the keyword "super" in a static method or block.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have an object that you only want 1 instance of, you use static.
A very good example of this is a database connection pool. You may want many connections but since creating connections is very expensive operation, so you want to reuse them. That is where the database pool comes in (a pool of database connections).
Scenario:
your database is configured for 110 connection and your database pool is configure for 100 connections. If you have 2 instances of the database pool, you can imagine the problems that can occur when one of those pools ask the database for its 111th connection
There is a very popular design pattern based on the static keyword it is called a singleton. Here is one article.
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html

Sean
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also: I would not think in terms of the advantages of one versus the other. Instead, they each have a distinct purpose.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rakesh lawr wrote:I want to know about the usage of STATIC keyword in a class/method/variable...


Campbell and Sebastian has already defined why the method should be static. I would only suggest to
remember when you make method static is that you must make sure there
is no chance you’ll want it to behave polymorphically.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic