masral masral - 2 months ago 7
Java Question

No enclosing instance type of or the main method cannot be declared stactic

My current dual problem is that I get a "the main method cannot be declared static error". When I try and remove it, the program will not run and when I try and give it to the parent type it comes back with highlighting the new LastDigitDistribution() saying the "No enclosing instance of type R113 is accessible" error. How can I fix this so that my program can run?

package ch11;

public class R113 {


public interface Sequence

{

int next();

}


/**
2
This class analyzes the distribution of the last digit of values
3
from a sequence.
*/
public class LastDigitDistribution

{
private int[] counters;

/**
Constructs a distribution whose counters are set to zero.
*/
public LastDigitDistribution()

{
counters = new int[10];
}
/**
Processes values from this sequence.
@param seq the sequence from which to obtain the values
@param valuesToProcess the number of values to process
*/
public void process(Sequence seq, int valuesToProcess)

{
for (int i = 1; i <= valuesToProcess; i++)
{
int value = seq.next();
int lastDigit = value % 10;
counters[lastDigit]++;
}
}
/**
Displays the counter values of this distribution.
*/
public void display()
{
for (int i = 0; i < counters.length; i++)
{
System.out.println(i + ": " + counters[i]);
}
}
}

public class RandomSequence implements Sequence

{
public int next()
{
return (int) (Integer.MAX_VALUE * Math.random());
}

}
public static class SequenceDemo
{
public static void main(String[] args)
{
LastDigitDistribution dist1 = new LastDigitDistribution();
dist1.process(new SquareSequence(), 1000);
dist1.display();
System.out.println();
LastDigitDistribution dist2 = new LastDigitDistribution();
dist2.process(new RandomSequence(), 1000);
dist2.display();
}
}

public class SquareSequence implements Sequence

{
private int n;

public int next()
{
n++;
return n * n;
}
}
}


I was looking it up and I believe this has something to do with nested inner classes but I can't figure out where I am going wrong with this.

Answer

Consider reading the section in the Java Tutorial on inner classes.

Your code is difficult to read without proper indentation, but it appears that:

Your main method appears to be nested inside the inner class called "SequenceDemo".

It needs to be outside of all of the innner classes, and be nested inside of LastDigitDistribution (the "outer" class).

In other words, the method "main" must be a member of LastDigitDistribution, not SequenceDemo.

Also most (maybe all?) of your inner classes need to be static inner classes. This modification of your code compiles and runs:

public class R113 {

    public interface Sequence

    {

        int next();

    }

    /**
     * 2 This class analyzes the distribution of the last digit of values 3 from
     * a sequence.
     */
    public static class LastDigitDistribution

    {
        private int[] counters;

        /**
         * Constructs a distribution whose counters are set to zero.
         */
        public LastDigitDistribution()

        {
            counters = new int[10];
        }

        /**
         * Processes values from this sequence.
         * 
         * @param seq
         *            the sequence from which to obtain the values
         * @param valuesToProcess
         *            the number of values to process
         */
        public void process(Sequence seq, int valuesToProcess)

        {
            for (int i = 1; i <= valuesToProcess; i++) {
                int value = seq.next();
                int lastDigit = value % 10;
                counters[lastDigit]++;
            }
        }

        /**
         * Displays the counter values of this distribution.
         */
        public void display() {
            for (int i = 0; i < counters.length; i++) {
                System.out.println(i + ": " + counters[i]);
            }
        }
    }

    public static class RandomSequence implements Sequence

    {
        public int next() {
            return (int) (Integer.MAX_VALUE * Math.random());
        }

    }

    public static class SequenceDemo {
    }

    public static class SquareSequence implements Sequence

    {
        private int n;

        public int next() {
            n++;
            return n * n;
        }
    }

    public static void main(String[] args) {
        LastDigitDistribution dist1 = new LastDigitDistribution();
        dist1.process(new SquareSequence(), 1000);
        dist1.display();
        System.out.println();
        LastDigitDistribution dist2 = new LastDigitDistribution();
        dist2.process(new RandomSequence(), 1000);
        dist2.display();
    }
}