Imagine you have 2 package and there are public classes named – Test.
- FirstPackage.Test
- SecondPackage.Test
each of them have instance variable – x.
in first case – int x = 2;
in second case – int x = 3;
i want to import FirstPackage.Test inside SecondPackage.Test and print x which has value 2.
My code:
package SecondPackage;
import FirstPackage.*;
public class Test {
int x = 3;
public static void main(String[] args){
Test t = new Test();
System.out.println(t.x);
}
}
But output is 3. How can I print 2?
>Solution :
use full class name
so change this
Test t = new Test();
to
FirstPackage.Test t = new FirstPackage.Test();