I'm trying to create a single class which contains several methods. The main method calls the different methods to initialise the array etc.
Because the main method is static, I can't call other non static methods from it. The only way I've managed to get it to work is something like the example below. However, I'm sure this is all wrong and there must be a way to do it without creating the methods that get called as static. I'm geussing that I should not place the method call in the main method, but then what does go in the main method?
What exactly is " public static void main (
String args[])" for, and what has to be inside that method, and what not? I also geuss I should declare the variables elsewhere. But where?
import javax.swing.*;
public class Mtry
{
public static void main (String args[])
{
int array[];
array = new int[10];
String output = "";
countArray(array);
output = writeArray(array, output);
JTextArea outputArea = new JTextArea();
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea, "Initializing an Array of int Values", OptionPane.INFORMATION_MESSAGE);
System.exit(0);
} // end main
static void countArray( int array2[])
{
for(int counter = 0; counter < array2.length; counter ++)
array2[counter] = counter ;
}
static String writeArray( int array2[], String output1)
{
for(int counter = 0; counter < array2.length; counter ++)
output1 += array2[counter];
return output1;
}
} // end class Mtry