Cameron said:
forget about the global variable comment i put in ..
public static void main(String[] args){
//set global var
double xCoord = 2.231;
double yCoord = 56.21;
global variables go inbetween the "public class myClass" and the main
constructor. (global vars come in quite handy when you move onto
swing )
These aren't global variables. As you explain them, they become instance
variables. Anyway, global variables are a Bad Thing.
And that's "Swing".
And class names are supposed to start with an upper-case letter.
You have all been so helpful, thank you n.n. Just one more question,
though. Can someone give me a basic (like...the most beginner-form)
example of calling a method? I think I have got down the basics of
writing a method, but I need a couple of examples of calling that
method.
There are two ways, depending on whether it's a static method or not.
If it's a static method, you call it with the class name, a dot. the method
name, and parentheses enclosing any arguments. If it's an instance method,
you call it with an object reference. a dot, then the method name and
parentheses, with arguments if any. Here's a sample class with a static and a
non-static (instance) method, declared 'public' so the client can use them:
public class Methodist
{
private double value;
/** Static method that takes an double and returns a value.
* @param xp <code>double</code> argument.
* @return double the result.
*/
public static double square( double xp )
{
return xp * xp;
}
/** Instance method that takes no arguments and returns the internal value.
* @return double the internal value.
*/
public double getValue()
{
return value;
}
/** Instance method that stores the argument in the internal value
* and returns nothing.
* @param val <code>double</code> to store in the internal value.
*/
public void setValue( double val )
{
this.value = val;
}
}
public class TestMethodist
{
/** Static method that process an array of <code>String</code>s.
* @param args <code>String []</code> arguments to process.
*/
public static void main( String [] args )
{
double sq = Methodist.square( Math.PI ); // invoke a static method
Methodist holder = new Methodist(); // an instance is required
holder.setValue( sq ); // invoke an instance method
sq = holder.getValue(); // invoke an instance method
System.out.println( "Internal value is "+ sq );
}
}