• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic