Inner class declaration and initialization

94 views Asked by At

I am used to code in C++, but have to convert a project from C++ to Java. In C++ using data structure is pretty much simple. I am trying to replicate the same thing, but such as a Java inner class and static nested class. After reading several examples online, and trying different versions, so far this is what I got:

public class Main {
  public static void main( String[] args ) {
  ...
    ClassOuter outerObj = new ClassOuter();

    ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();
  }
}

class ClassOuter{

  public static class DataInner{
    public int x;
  }
  ...
  protected void getNo()
  { value.x=Integer.parseInt("493"); 
  }
}

However, when I try to compile, it gives me the error:

$ javac -cp "./" Main.java
Main.java:15: error: '(' expected
    ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();

Any clue about what is missing here?

2

There are 2 answers

3
Eran On BEST ANSWER
ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();

This syntax applies to inner classes (i.e. non static nested classes). If that's what you want, remove the static keyword from public static class DataInner.

EDIT :

Also change

ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();

to

ClassOuter.DataInner value = outerObj.new DataInner();

You don't specify the outer type when using an enclosing instance to initialize the inner instance.

And the line outerObj.value.x=Integer.parseInt("493"); is not valid inside your outer class's getNo() method, since outerObj and value are local variables known only to your main method.

If you wish your outer instance to update any of its inner instances, it must obtain a reference to it. Here's one way to do it :

public class Main {
  public static void main( String[] args ) {
  ...
    ClassOuter outerObj = new ClassOuter();    
    ClassOuter.DataInner value = outerObj.new DataInner();
    outerObj.setInner (value);
  }
}

class ClassOuter{

  public static class DataInner{
    public int x;
  }
  ...
  private DataInner inner = null;
  public void setInner (DataInner inner) {
      this.inner = inner;
  }
  protected void getNo()
  {
      inner.x=Integer.parseInt("493"); 
  }
}
0
AudioBubble On

If DataInner must be static class:

public class Main {
    public static void main(String[] args) {
        ClassOuter outerObj = new ClassOuter();
        ClassOuter.DataInner value = new ClassOuter.DataInner();
    }
}

class ClassOuter {
    public static class DataInner {
        public int value;
    }
}

It this case, DataInner has no reference to the ClassOuter instance.

If DataInner must not be static class.

public class Main {
    public static void main(String[] args) {
        ClassOuter outerObj = new ClassOuter();
        ClassOuter.DataInner value = outerObj.newInner();
    }
}

class ClassOuter {
    public class DataInner {
        public int value;
    }
    public DataInner newInner() {
        return new DataInner();
    }

In this case, DataInner has reference to the ClassOuter instance (ClassOuter.this).