- Yes if it is same Class member.
If Class has static/class member(Static member also known as class members) we can access in following ways:
- Directly
If your trying to access static member with in same class you can call it off with out any reference.
- With Class name
If static member is not present in same class We must access with with Class name - With Object or Object reference
Object:
If A is a Class then new A() will be the Object.
Object reference:
A a = new A();
Here "a" is a object reference.
Eg:
class A{ static int a = 10; static void callMe(){System.out.println("I'm from static member");} public static void main(String args[]){ System.out.println(a); // Valid --->; 1System.out.println(A.a); // Valid--->; 2System.out.println(new A().a); // Valid--->; 3System.out.println(A.callMe());// ValidSystem.out.println(new A().} }callMe());// Valid
How many ways we can access instance members of Class?
There are two ways to access.
One is, object and other one is object reference.
classA{ int a = 10; void callMe(){System.out.println("I'm from instance member");} public static void main(String args[]){ System.out.println(a); // not-validSystem.out.println(new A().a); // ValidSystem.out.println(A.callMe());// not-validSystem.out.println(new A().A ob = new A();callMe());// ValidSystem.out.println(ob.a);} }System.out.println(ob.callMe());
There is big difference between object and object reference[Check it out].
No comments:
Post a Comment