Nested Class not working properly

Started by
4 comments, last by Dragonsoulj 10 years, 5 months ago

Can somebody help me figure out the problem in the following code sample. My IDE says I have a mistake but I can't detect it. I am having trouble with the Inner Class.

I am using eclipse if it helps.




public class Outer {

    

    int nums[];

    

    Outer(int n[])

    {

        nums = n;

    }

    

    void Analyze()

    {

        Inner inOb = new Inner();

        

        System.out.println("Minimum: " + inOb.min());

        System.out.println("Maximum: " + inOb.min());

        System.out.println("Average: " + inOb.min());

        

    }



    

 class Inner()

    {

    

        int min()

        {

            int m = nums[0];

            for(int i = 1; i < nums.length; i++)

                if(nums[i]<m)

                    m = nums[i];

            return m;

        }

        

        int max()

        {

            int m = nums[0];

            for(int i = 1; i < nums.length; i++)

                if(nums[i] > m)

                    m = nums[i];

            

            return m;

        }

        

        int avg()

        {

            int a = 0;

            for(int i = 0; i < nums.length; i++)

                a += nums[i];

            

            return a / nums.length;

        }

    }

}
Advertisement

I didn't think you nested classes. I thought you used something like extend.

Instead of this:


class Inner() {

}

you need this:


class Inner {

}

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

I didn't think you nested classes. I thought you used something like extend.

Java requires only on class per file, but inside that class you can declare other classes. Check out the Point2D.Double class from Java to see an example:

http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Point2D.Double.html

Extend is for using inheritance, which is something different.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Thanks @Glass_Knife, I feel silly now because the error looks obvious now. Guess that's what I get for coding late Lol.

I didn't think you nested classes. I thought you used something like extend.

Java requires only on class per file, but inside that class you can declare other classes. Check out the Point2D.Double class from Java to see an example:

http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Point2D.Double.html

Extend is for using inheritance, which is something different.

It's been a while since I worked with Java. Guess I need a refresher project.

This topic is closed to new replies.

Advertisement