October 30th, 2005, 02:40 PM
|
#8 (permalink)
|
|
Hit By Ban Bus!
Join Date: Oct 2005
Location: UK, soon to be home in the USA
Posts: 1,338
|
Quote:
Originally Posted by dragonlady
public static void main means that you are writing an executable program. So in the Amoebamath program, everytime you run it, every statement in the "main" function will be executed. You can have classes without a main method, though, like the following example.
This does not have a main method, so if you try to run it, nothing will happen, because you have not provided any instructions for it to run. You could use the Point class in a program with a main method, if you wanted to. For instance:
Quote:
public class Demo{
public static void main(String args[]){
Point p = new Point(2, 2);
System.out.println(p.getX());
System.out.println(Point.getDifference(4, 2);
}
}
|
So now when I run this program, I am actually running an executable program. By contrast, try running Point and you'll see that nothing happens.
Static methods require an instance of an object to run. See Point's getX(); if I haven't created a Point object, then I don't have a Point to get x from. Imagine if I asked you the area of a circle that doesnt exist; it doesnt make sense to ask that question. Nonstatic methods dont require that an object be created. I wrote a method called getDifference, to which I provided two ints. The ints I provided had nothing to do with a Point object. Main methods are static because by definition they are run before any objects are created. Void means that the method does not return anything. Does that helpd clear things up? Good luck 
|
 Christ NO! Can you believe I have actually taken...and passed...not only a Java programming course, but a VB programming course as well, and I used to be able to do C as well?
Christ, the expression 'use it or lose it' was NEVER MORE TRUE!!!!!!!!!!
|
|
|