Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

non static variable in static class  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
normally a static method can only access static variable..but in public static void main() the main class access non static variable..how is it so..


class one
{
public static void main(String args[])
{
int a=5;//non static variable
int b=3;// non static variable
int c=a+b;
System.out.println(c);
}
}
 
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

It's true that a static method cannot access non-static class member variables.

But in your example a, b and c aren't such member variables. Instead they are local variables which are always accessible (only) within the method in which they are declared! It wouldn't make sense if you could just declare local variables but couldn't use them

Marco
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!