why can main access outside classes that are not static but cannot access nested non static classes??
Non static nested class
please elaborate your question
Yeah
So i mean I can easily create an object of any non nested class in main , right?
But in nested class that class has to be static so that I can access it in main
Why cant a non static nested class can be accessed in main
@bhavik911
the inner class object is always associated with the outer class object.
I will give an example of how is this access
public class Alpha {
public Alpha() {
// TODO Auto-generated constructor stub
}
public void print() {
System.out.println("Hey Alpha");
}
public class Beta {
int be = 10;
public Beta() {
// TODO Auto-generated constructor stub
}
public void print() {
System.out.println(this.be);
}
}
}
client of Alpha :
public class AlphaClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
Alpha al = new Alpha();
Alpha.Beta ab = al.new Beta();
}
}