How to refer from class1 to a class2 instance variable?

For example, from here we turn:

public class Main { public static void main(String[] args) { System.out.println(object1.a); //ошибка } } } 

And the class in which the object1 variable is declared is in the same package:

 public class Solution { int a = 555; public static void main(String[] args) { Solution object1 = new Solution(); } } 

    1 answer 1

    Like so.

     public class Main { public static void main (String[] args) { Solution object1 = new Solution(); System.out.println(object1.a); } } 

    The scope of the variable. The main method does not see your object1 variable object1 and this is where an error occurs.