• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem in static variable

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
can anyone tell me why this program is giving compiler error..

class staticvartest1
{
//static int x;
public static void main(String[] args)
{
static int x=10;//This is causing error
x=x+1;
System.out.println("Hello World!");
System.out.println(x);
}
}
}
However this program runs...
class staticvartest1
{
static int x;
public static void main(String[] args)
{
//static int x=10;
x=x+1;
System.out.println("Hello World!");
System.out.println(x);
}
}
}
gives output of 1

<marquee>Plz help me.......</marquee>

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You cannot have a modifer (static) for local variables.
Your second example compiles and runs because you have declared x to be a member variable.
------------------
~James Baud
Talk, does not cook rice. - Chinese Proverb
 
atin sehgal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic