Cannot pass array value from class containing main method to different classes. Why? Please Help Really Wanna Know
<p>Our teacher asked us to create a prog. that would replicate the working of a stack based on the principle of LIFO and would also indicate stack overflow and empty stack..
what i did was created three classes. Retrievee class containing main method which takes input from user using String[] args. other class Push uses the values in Retrive class Push should accept the values according to its size and checks if values exceeds size which it would and states overflow and proceeds to class Pop which uses the values in Class push rather then retrieve .</p>

<pre><code>class Push extends Retrivee
{
  int i;
  Push()
  {
    String push[]= new String[10];
    //int push[]=new int[10];
    for(i=0;i&lt;12;i++)
    {
      //Retrivee ele= new Retrivee();
      push[i]=a[i];
      System.out.print("value in Push["+i+"]="+push[i]);
      if( i == 9 )
         {
           System.out.print("Stack for push has Overflown");
         }
    }
  }
}

class Pop extends Push
{
  int i;
  Pop()
  {
    String pop[]= new String[10];
    //int pop[]= new int[10];
    for(i=10;i&gt;=-1;i--)
    {
      //Push ele1= new Push();
      pop[i]=push[i];
      System.out.print("value in Pop["+i+"]="+pop[i]);
      if(i == -1)
      {
       System.out.print("Stack for pop is empty");
      }
    }
  }
}
public class Retrivee
{
public static void main(String args[] )
{
System.out.println("please enter 12 elements");
String a[] = new String[13];
//int a[]=new int [13];
a[1]=args[1];
a[2]=args[2];
a[3]=args[3];
a[4]=args[4];
a[5]=args[5];
a[6]=args[6];
a[7]=args[7];
a[8]=args[8];
a[9]=args[9];
a[10]=args[10];
a[11]=args[11];

/*
 a[0]=Integer.parseInt (args[0]);
 a[1]=Integer.parseInt (args[1]);
 a[2]=Integer.parseInt (args[2]);
 a[3]=Integer.parseInt (args[3]);
 a[4]=Integer.parseInt (args[4]);
 a[5]=Integer.parseInt (args[5]);
 a[6]=Integer.parseInt (args[6]);
 a[7]=Integer.parseInt (args[7]);
 a[8]=Integer.parseInt (args[8]);
 a[9]=Integer.parseInt (args[9]);
 a[10]=Integer.parseInt (args[10]);
 a[11]=Integer.parseInt (args[11]);
 */

 /*Push push1 =new Push();
  Pop pop1 =new Pop();

 */
  }
 }
</code></pre>

<p>at the time of compile i got the error</p>

<pre><code> Retrivee.java:11:error :cannot find symbol
    push[i]=a[i];
    symbol: variable a
    location:class Push
 Retrivee.java:31:error :cannot find symbol
    pop[i]=push[i];
    symbol: variable push
    location:class Pop
</code></pre>

<p>so i tried every thing atlast thought may be values taken from String[] args cannot be passed to another classes.so improved the code little bit.</p>

<pre><code>class Push //extends Retrive
{
    public Push()
    {
        int i;
        String push[]= new String[10];
        //Retrive ele= new Retrive();
        try
        {
            for(i=0;i&lt;13;i++)
            {
                push[i]=Retrive.a[i];
                System.out.println("value in Push["+i+"]="+push[i]);
            }
        }
        catch(Exception e)
        {
            System.out.println("Stack for push has Overflown");
        }
    }
}

class Pop //extends Push
{
    public Pop()
    {
        int j;
        String pop[]= new String[10];
        //Push ele1= new Push();
        try
        {
            for(j=9;j&gt;=-1;j--)
            {
                pop[j]=Pop.push[j];
                System.out.println("value in Pop["+j+"]="+pop[j]);
            }
        }
        catch(Exception ev)
        {
            System.out.println("Stack for pop is empty");
        }
    }
}

public class Retrive 
{
    public static void main(String args[])
    {
        String a[] = new String[13];
        for(int k=0;k&lt;13;k++)
        {
            int d[]=new int[13];
            d[k]=2*k;
            a[k]=Integer.toString(d[k]);
            System.out.println("a["+k+"]= "+a[k]);
        }
    }
}
</code></pre>

<p>but again endup with same errors</p>

<pre><code> Retrive.java:12:error :cannot find symbol
    push[i]=Retrive.a[i];
    symbol: variable a
    location:class Retrive
 Retrive.java:34:error :cannot find symbol
    pop[i]=Pop.push[i];
    symbol: variable push
    location:class Pop
</code></pre>

<p>then i try this same code but in just single class that contained the main method..</p>

<pre><code>public class Retrivee
{
    public static void main(String args[] )
    {
        String a[] = new String[13];
        for(int k=0;k&lt;13;k++)
        {
            int d[]=new int[13];
            d[k]=2*k;
            a[k]=Integer.toString(d[k]);
            System.out.println("a["+k+"]= "+a[k]);
        }
        int i;
        String push[]= new String[10];
        try
        {
            for(i=0;i&lt;13;i++)
            {
                push[i]=a[i];
                System.out.println("value in Push["+i+"]="+push[i]);
            }
        }
        catch(Exception e)
        {
            System.out.println("Stack for push has Overflown");
        }
        int j;
        String pop[]= new String[10];
        try
        {
            for(j=9;j&gt;=-1;j--)
            {
                pop[j]=push[j];
                System.out.println("value in Pop["+j+"]="+pop[j]);
            }
        }
        catch(Exception ev)
        {
            System.out.println("Stack for pop is empty");
        }
    }
}
</code></pre>

<p>and at last it worked but the question arises why did not worked in the above two cases... please explain.. and if it could have worked please tell how and where the changes should have been made....</p>

<p>and last thin i do wanna ask is why cant we set the size of String args[] in main method by writing like this </p>

<pre><code>  public static void main(String args[] = new String[10]) 
  {
  }
</code></pre>

<p>and following the first example i wrote a small code</p>

<pre><code>import java.util.*;
public class MyScan 
{
    public static void main(String[] args) 
    {
        //String[] args = new String[10];
        String[] in = new String[4];//= {"1 a 10 . 100 1000","12","2112"};
        in[0]= "1"; //args[0];
        in[1]= "2"; //args[1];
        in[2]= "3"; //args[2];
        String accum;
        for(int x = 0; x &lt;3; x++)   
        {
            Scanner s = new Scanner(in[x]);
            accum = s.nextLine();
            System.out.println(accum);
        }
    }
} 
</code></pre>

<p>when i wrote</p>

<pre><code> in[0]= args[0];
 in[1]= args[1];
 in[2]= args[2];
</code></pre>

<p>it threw </p>

<pre><code>  Exception in thread"main" java.lang.ArrayIndexOutOfBoundsException:0
  at MyScan.main&lt;MyScan.java:9&gt;
</code></pre>

<p>but the above initialization worked when i used in another prog. but somewhat in diff. way</p>

<pre><code>            String a= args[0];
    String b= args[1];
</code></pre>

<p>Sorry for such long ques but no one was able to answer the questions</p>
