简体   繁体   中英

How do you instantiate an inner class of a static nested class?

This is my class

public class XYZ{

    public static void main(String[] args) {
        int n = 5;
        Object myObject;
       //somehow call returnN(n);
    } //end main

    static class Inner{
        private class Private{
            private int returnN(int n){
                return n;
            }
        }
    }//end of Inner


}

I am trying to call returnN from main. I've tried myObject = new XYZ.Inner.Private(); but this does not appear to be the correct instantiation.

Private is not static . Therefore you need an instance of Inner before you can create a Private . The correct syntax is

new XYZ.Inner().new Private().returnN(3);

Alternatively, you can make Private static and then you can just do

new XYZ.Inner.Private().returnN(3);

Sean: why did you delete your recent question after Alain put in effort to answer it? I will post it here so that it at least has an internet presence, but you should re-open it, and up-vote Alain's answer, at least for the effort he put into answering it.


Question

I am supposed to implement a class, when I have lines like this:

double v = className.functionName().main(a,b);

And

className.output.display(v);

It is not clear to me what this hierarchy is supposed to look like in the className class.

What does it mean to call main() from a function call? Calling a main function from a function? Why possibly isnt this just functionName(a,b)?

And the point of using .output.? Is this supposed to be a void with a sub-function display()?


Alain's Answer

Answer by Alain O'Dea

output is a field. There is no such thing as a sub-function or method, there are just methods on objects. The display() method is on the object field of the classname object.

The main() method here is just a method on the object returned by functionName() . There is no special meaning from being called main() .

I would infer something like this from the lines given:

I'm going to assume that className is an instance of ClassName used like this:

public class Main {
    public static void main(String[] args) {
        ClassName className = new ClassName();
        Object a = new Object(); // replace with correct a
        Object b = new Object(); // replace with correct b
        double v = className.functionName().main(a,b);
        className.output.display(v);
    }
}

And ClassName would be defined as follows:

public class ClassName {
    public Output output = new Output;
    public Mainable functionName() {
        return new Mainable();
    }
}

Output.java:

public class Output {
    public void display(double v) {
        // implement display here
    }
}

Mainable.java:

public class Mainable {
    public double main(Object a, Object b) {
        return 0.0d; // put actual impl here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM