4

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?

Community
  • 1
  • 1

2 Answers2

5
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"); 
  }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    @ThiagoOliveira ` ClassOuter.this.x=Integer.parseInt("493");` was wrong and I edited that. I mistakenly thought that line was in your inner class, but it was in your outer class. See my last edit. – Eran Jun 14 '15 at 06:59
  • 1
    @ThiagoOliveira There can be multiple inner class instances all having the same outer enclosing instance. In order for your outer instance to update any of its inner instances, it must obtain references to them. – Eran Jun 14 '15 at 07:00
  • thanks again. as a matter of fact what I need is something like: ClassOuter.this.x=Integer.parseInt("493"); basically the idea is to have my class that contains instead of several data spread separately, put all data together. The class DataInner is indeed inside ClassOuter. Anyway that would be possible to initialize or have the object ("value") inside it? – Thiago Oliveira Jun 14 '15 at 07:11
1

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).