• 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 Variable Problem

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why in a static method for ex main() method we cannot
declare static variable ?
public static void main(String args[])
{
static int i;
}
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
STATIC VAR. CANNOT BE DECLARED LOCAL.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,
The reason for static variable not allowed to be declared locally is on the fact that static variables are "class" variables so they are accessible to all the instances of the class. Now let
us consider a scenario where you have a method that declares a static variables (i.e., the scope of this variable would be local as it is declared locally this contradicts the basic purpose of defining a variable as static) , an object calls this method does same manupulation and when the method call is returns the static variable must die as it is now of scope, else we are in more serious trouble as this hanging static variable may conflict/temper with other variable.
I guess this serves the purpose of this discussion.
Cheers
Ravindra Mohan
 
Attractive, successful people love this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic