• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods

 
Masha Stekker
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Often what you do is in main, you create an instance of the class your writing. you can then use that to call the methods...

note: i just kind of fired this off, but i hope it give you the idea...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mrs Pepperpot,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Titles are not satisfactory as first names.
Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic