0
Hello Guys I have a ques
class A { class B { B b; } } in above example B b; is a ref variable or instance ....and if it s ref variable then it point to which object & as u see i cannot created its object so what would be the benefit of this ref var
3 odpowiedzi
+ 1
B b; // Here b is a reference to class B whereas the whole class B is like an instance variable of class A.
This is a simple concept of inner classes which are non-static in your case so you have to create an instance of the outer class A and then create the instance of the inner class B unlike static where you can directly create an instance for the inner class.
We generally use nested or inner classes -
1) to have more readable and maintainable code.
2) also the inner class objects can access all the data members of the class enclosing it.
Here is a simple example.
https://code.sololearn.com/cIT32cxCQC0k/?ref=app
+ 3
//Java
class A {
class B { B b; }
}
public class Prg {
public static void main(String[] args) {
A.B ab = new A().new B();
System.out.println(ab); // A$B@7a81197d
System.out.println(ab.b); // null
ab.b = ab;
System.out.println(ab.b ); // A$B@7a81197d
}
}