0

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.

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

6 Comments

"Private is not static" -- I know that it isn't static, but Inner is, and to my understanding, a static class is a sort of "shared instance," so using "()" isn't needed, so why can't I do new XYZ.Inner.Private()?
Also why does it allow me to do myObject = new XYZ.Inner().new Private(); but then I cannot do myObject.returnN(3); ?
A static nested class is not a shared instance. A static nested class is really just like a top-level class except it happens to be defined inside another class. On the other hand, an instance of an inner class can be thought of as belonging to the enclosing instance.
You can't do myObject.returnN(3); because the type of myObject is Object, and Object doesn't have a method returnN.
You can do Inner.Private myObject = new Inner().new Private();. myObject.returnN(3);
|
0

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
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.