File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static methods" Watch "static methods" New topic

static methods

Masha Stekker

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 = "";

output = writeArray(array, output);

JTextArea outputArea = new JTextArea();

JOptionPane.showMessageDialog(null, outputArea, "Initializing an Array of int Values", OptionPane.INFORMATION_MESSAGE);

} // 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

Joined: Oct 02, 2003
Posts: 11955

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

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dirk Schreckmann

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:
subject: static methods
It's not a secret anymore!