public class Ex7 {
private int fld;
private void meth(int val) {
fld = val;
System.out.println(" meth() -> fld = " + fld);
}
public class Ex7Inner1 {
void operateOnFld() {
fld = 12;
}
void operateOnMeth() {
meth(10);
}
public void bar() {
System.out.println(" bar() ");
}
}
class Ex7Inner2 {
Ex7Inner1 i1 = new Ex7Inner1();
// how to call i1.bar() ??
i1.bar();
}
}
public class Ex7 { private int fld; private void meth(int val) { fld =
Share
Your problem is that you need to call
i1.bar()inside a function. For exampleIn the future, you may find that people are able to be more helpful if you include the error you get in your question, which I’ll do now. When you try to compile the code your way, you get an error that looks like
This is because the only thing you can do outside of a method (like you originally had it) is declare variables. So it was expecting an “identifier” by which it meant “the name of the variable you are declaring”. So if you had said
then
swould have been the identifier.