-1

I just started with java and I create a class Range() inside my superclass with a method inside makeRange but when I tried to access to that method throws an error. Whats wrong here?

Here is my code...

public class iAmRichard {

        class Range{
            int[] makeRange(int upper, int lower){
                int[] ary = new int[(upper - lower)+1];
                for(int i = 0; i > ary.length; i++ ){
                    ary[i] = lower++;

                }
                return ary;
            }

        }

    public static void main(String[] args) {
            int foo[];
            Range fui = new Range();
            foo = Range.(here do not apear makeRange method)
fabian
  • 80,457
  • 12
  • 86
  • 114

4 Answers4

1

You're creating an inner class here called Range. I don't believe that's what you intended to do, but I'll answer it as stated.

You're referring to this class in a static context, and the inner class can't be referenced with a static context. To address that, you need to make the change to Range: make it static.

public class iAmRichard {

    static class Range {

    }
}

Further, you're already getting an instance of Range, so all you need to do is use it.

foo = fui.makeRange(1, 10);

If you elected to only create a class called Range, you wouldn't have to deal with any inner classes at all, which I think would be the cleaner approach here.

public class Range {
    int[] makeRange(int upper, int lower) {
        int[] ary = new int[(upper - lower) + 1];
        for (int i = 0; i > ary.length; i++) {
            ary[i] = lower++;

        }
        return ary;
    }
    public static void main(String[] args) {
        int foo[];
        Range fui = new Range();
        foo = fui.makeRange(1, 10);
    }

}
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

To access a method without creating an instance you have to declare it static. In your case you have also to declare the class Range as static.

Or you can just use the instance you already have with a few changes:

iAmRichard richard=new iAmRichard();
Range fui=richard.new Range();
foo = fui.makeRange(...);

Note tha you need an instance of iAmRichard to create a Range.

gdros
  • 393
  • 2
  • 10
  • It's not that you're wrong...it's just that the advice here isn't entirely helpful. Hint: isn't there an instance floating around? – Makoto Oct 23 '15 at 16:41
  • @Makoto: not really. Since the class `Range` isn’t `static`, the creation code before that already fails as there’s no outer instance. – Holger Oct 23 '15 at 16:42
  • @Holger: That's not what this is answering, though. This is saying that the method needs to be made static, which isn't enough to solve the problem. – Makoto Oct 23 '15 at 16:43
  • @Makoto: exactly. In order to make the method `static`, the class has to be `static` as well, so *both* solution approaches don’t work as long as the class isn’t `static`… – Holger Oct 23 '15 at 16:44
  • @Holger: Again, that's not what's being answered here. The only thing that's being addressed is that the method should be static, which is *also* misleading. As written, sure, that answers the question, but that's a symptom of the *actual* problem(s) with the code. – Makoto Oct 23 '15 at 16:47
  • foo = fui.makeRange(10, 1); and then... no enclosing instance of type iAmRichard is accsesible must qualify.... – ignacio pugliese Oct 23 '15 at 16:50
0

Since the call is made from a static block in a static way(No instance is used for calling makeRange method) we need to have the called method to be either static or we need the object of the class to call instance methods.

Vivek Singh
  • 2,047
  • 11
  • 24
0

statically you can use this example to access your method. Here is a link for more information on static methods.

public class IAmRichard {

public static void main(String[] args) {
        int foo[];
        foo = Range.makeRange(10,1);
    }

    static class Range{
        static int[] makeRange(int upper, int lower){
            int[] ary = new int[(upper - lower)+1];
            for(int i = 0; i > ary.length; i++ ){
                ary[i] = lower++;

            }
            return ary;
        }
    }
}
Community
  • 1
  • 1
Ryan
  • 1,972
  • 2
  • 23
  • 36
  • but make it statc not will make the method stay on memory? – ignacio pugliese Oct 23 '15 at 16:54
  • a static modifier means that the method makeRange belongs to that class and not a instantiated object. ill add a link for more information of the difference between static and non static methods in a edited answer. – Ryan Oct 23 '15 at 17:00