Object refrence not able to access class values

class 1 :-package DEMON;

public class DEMO1 {

public static void main(String[] args) {
	String name;
    int age;
}

}

class2–
package DEMON;

public class DEMO2 {

public static void main(String[] args) {
	DEMO1 d1=new DEMO1();
	System.out.println(d1.age);

}

}

Your code is wrong. You have declared name and age inside the main function which makes these variables local variables and not instance variables. Define them outside the main in Class Demo1 and then your program should work just fine.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.