-3

Here are the code. Error happens at 2rd line.

public class TestStack1 {

    public static void main(String[] args) {
        FixCapacityStackOfString fcstack = new FixCapacityStackOfString(100);
        for (int i = 0; i < args.length; i++) {
            fcstack.add(args[i]);
        }
    }


    class FixCapacityStackOfString {

        public FixCapacityStackOfString(int a) {
            list = new String[a];
        }

        private int size;
        private String[] list;


        public int size() {
            return size;
        }

        public boolean isEmpty() {
            if (size == 0) {
                return true;
            } else {
                return false;
            }
        }

        public String push() {
            return list[--size];
        }

        public void add(String s) {
            list[size++] = s;
        }
    }
}

Because it uses the args,I can't move the main method into a method in public class.How can I correct it?

user207421
  • 305,947
  • 44
  • 307
  • 483
jerrywyn1
  • 3
  • 2
  • What error? Third line of what? What uses `args`? – user207421 Feb 23 '19 at 07:31
  • please provide more code along with error – kamlesh pandey Feb 23 '19 at 07:36
  • The `main` method is an entry point into your application when it’s started and not some method you can call or otherwise include in your own classes if that is what you are trying to do. – Joakim Danielson Feb 23 '19 at 08:23
  • You should study about objects, classes and methods. You have not posted your full code, but I suspect `FixCapacityStackOfString` is a nested class. Since this nested class is not `static`, you need an instance of the enclosing class. – MC Emperor Feb 23 '19 at 08:40
  • Possible duplicate of [strange Cannot be referenced from a static context error in Java](https://stackoverflow.com/questions/43164735/strange-cannot-be-referenced-from-a-static-context-error-in-java) – MC Emperor Feb 23 '19 at 08:43
  • Possible duplicate of [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Alex K Feb 23 '19 at 18:54

2 Answers2

0

Your code looks like this:

class TestStack1 {
    public static void main(String[] args) { ... }
    class FixCapacityStackOfString { ... }
}

And you use FixCapacityStackOfString in main. But FixCapacityStackOfString is not a static class: it requires an instance of TestStack1. You don't have this instance in static main method. So just declare FixCapacityStackOfString as static:

static class FixCapacityStackOfString { ... }
0

This is a great question which shows the magic of "static" keyword.

Your program can be executed in three ways:

First Way: When the class FixCapacityStackOfString is a nested class.

class ClassName
{
public static void main(String[] args) {

    ClassName obj = new ClassName();
    FixCapacityStackOfString fcstack =   obj.new FixCapacityStackOfString(100);
}

class FixCapacityStackOfString {

    public FixCapacityStackOfString(int a) {
        list = new String[a];
    }

    private int size;
    private String[] list;
    }

}

In this, the invocation of a constructor which is, non-static method, requires an instance of the class in which it is defined. Since you're calling from static method main, this is how you have to invoke.

Second Way: If you class FixCapacityStackOfString is outside the main class

class ClassName
    {
    public static void main(String[] args) {

        FixCapacityStackOfString fcstack =   new FixCapacityStackOfString(100);
    }


    }

class FixCapacityStackOfString {

        public FixCapacityStackOfString(int a) {
            list = new String[a];
        }

        private int size;
        private String[] list;
        }

Third Way: Declare the class FixCapacityStackOfString as static inside first class.

class ClassName
{
public static void main(String[] args) {

    FixCapacityStackOfString fcstack = new FixCapacityStackOfString(100);
}

static class FixCapacityStackOfString {

    public FixCapacityStackOfString(int a) {
        list = new String[a];
    }

    private int size;
    private String[] list;
    }

}