java - Inner class Constructor is not showing required output -


i expected innerclass value 8 code showing "0", why value not assigned inner class constructor

public class testit {  public static void main(string[] args) {     test obj = new test(9, 8);     test.test2 obj2 = obj.new test2();     obj2.show();     // todo code application logic here } } 

class test {  private int a; private test2 d;  public test() {     = 0; }  public test(int aa, int b) {     = aa;     d = new test2(b); }  public class test2 {      private int b;      public test2() {         b = 0;     }      public test2(int bb) {         b = bb;     }      public void show() {         system.out.println("outer variable " + + " inner variable " + b);     } } } 

your mistake in main method itself

public static void main(string[] args) {     test obj = new test(9, 8);     test.test2 obj2 = obj.new test2();     obj2.show();     // todo code application logic here } 

"obj" new instance of "test" class. "obj2" new instance of test2 (even if use obj reference having new instance).

if want retrieve built value, add getter test.d attribute , change "test.test2 obj2 = obj.new test2();" "test.test2 obj2 = obj.getd();"

you should print :

outer variable 9 inner variable 8 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -