Reputation: 75
Here I am trying to access non-static variable 'a' of Test class in main. It is
accessible why?
class Test{
public static void m1(){}
public void m2(){}
public int a=20;
public static int b=30;
public static void fun(){
System.out.println(a); //it gives an error because variable a is non-static
System.out.println(b);
}
}
class Test1{
public static void main(String args[]){
Test s=new Test();
s.b=10;
s.a=20;
System.out.println(s.a); /*why this statement not giving an error even variable'a'is
non-static, we are accessing it in static main() method */
}
}
Upvotes: 0
Views: 342
Reputation: 3658
You're not getting any errors because you're accessing a member variable on an instance of Test
. Example:
public class Foo{
private static int bar;
private int foobar;
public static void main(String[] args){
System.out.println(bar); // possible,because bar is static
System.out.println(foobar); // illegal since you're trying to access an instance variable from a static context
System.out.println(new Foo().foobar); // possible since you're accessing the foobar variable on an instance of Foo
Upvotes: 0
Reputation: 2646
I think you are mixing non-static and private.
All that is required to access a non-static public member is to hold an instance of the class.
If you don't want the member to be accessible, make it private/protected.
Upvotes: 0
Reputation: 4022
Variable a is accessible because it is public in your Test class, and you can access it from the static main method because you have created an instance of Test called s.
Upvotes: 1
Reputation: 280778
You can't use unqualified references to instance variables in a static method, because those implicitly refer to a this
instance that doesn't exist. When you specify s.a
, you are referring specifically to the a
field of object s
, rather than some non-existent this
, so Java finds the field and lets you access it.
Upvotes: 1
Reputation: 45060
Non-static variables should be accessed using the class instance object and that is what you've done, which is why there is no error thrown by the compiler.
In fact, it is b
which has been accessed wrongly(though it wont throw an error, but will show an warning saying static variables should be accessed in a static manner). Since it is static, you need to access it using the Class name.
Test.b = 20;
Also, you get the error in fun()
method because you've tried to access a non-static
field a
inside a static
context which is the fun()
method. Even though main()
is a static
method, you've access a
using an instance of Test
class which is the right thing to do.
Upvotes: 0
Reputation: 70929
You are accessing the non-static variable via an instance(s
) which is perfectly legal as the variable is public. Being static or non-static has nothing to do with access restrictions - the only thing that changes is if you need an instance to use it or not.
Upvotes: 0
Reputation: 5008
You're creating an instance of Test (s object) and accessing its public property. It's fine. It should work this way. The "static way" to access this property would be like this:
int x = Test.a;
And it will NOT work because your s attribute isn't static.
Upvotes: 0
Reputation: 13844
System.out.println(s.a);
it will not give error because you are creating an object of test class and calling its variables
You might be confused about accessing static and non static variables in main method(static method for example).Consider this
you can directly write System.out.println(Test.b);
but you can not write System.out.println(Test.a);
as a
is not static
Upvotes: 0