aspose file tools
The moose likes Beginning Java and the fly likes static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "static methods" Watch "static methods" New topic
Author

static methods

Masha Stekker
Greenhorn

Joined: Mar 19, 2004
Posts: 15
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

Joined: Oct 02, 2003
Posts: 10040
    
    6

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...


Never ascribe to malice that which can be adequately explained by stupidity.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: static methods
 
Similar Threads
code segment question #2
method takes longer each time
Large arrays
loops that slow down
doubt in pass by refrence