Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes non static variable in static class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "non static variable in static class" Watch "non static variable in static class" New topic
Author

non static variable in static class

mari maris
Greenhorn

Joined: Mar 22, 2008
Posts: 7
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

Joined: Mar 07, 2007
Posts: 1220

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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: non static variable in static class
 
Similar Threads
non-static methods
How come?
instance variables and methods
Access Modifiers
Help needed.....