• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static methods

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic