Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

non static variable in static class  RSS feed

 
mari maris
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);
}
}
 
Marco Ehrentreich
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!