Let's not worry about static/non-static for a second, that's another can of worms. Let's think about what kind of programs you've mostly built so far; perhaps you've designed a program like calculating the distance between two (x,y) coordinates.
public static void main(String[] args) {
double x1 = 4.0;
double y1 = 3.0;
double x2 = 4.0;
double y2 = 4.0;
double x = Math.pow(x2 - x1, 2);
double y = Math.pow(y2 - y1, 2);
double distance = Math.sqrt(x+y);
System.out.println("The distance is" + distance);
}
Now, what happens if you want to use that code in a more complex program, like a video game to determine if your character is colliding into a wall? Now you have 3 coordinates(A and B are the Wall and C is the Character) and you'll need to find out the distance between all three coordinates (AB, AC, and BC). Why? Because if AB == AC + BC, then our character has ran into a wall! Here's a video explaining why this will work by yours truly: Collision Detection of 2D Points
Do I want to have to repeatedly type the same formula? Or waste time copying and pasting? No, I'm lazy. That's why I program the computer to do things for me.
What I CAN do, however, is design tiny, little programs that can run inside my big, main program. These are called methods.
public static double distance(double x1, double y1, double x2, double y2) {
double x = Math.pow(x2 - x1, 2);
double y = Math.pow(y2 - y1, 2);
double dist = Math.sqrt(x+y);
return dist;
}
Now, notice that I did two things differently.
One, I named my variable dist instead of distance; it's just good programming practice not to name your variables the same as your method.
Two, I threw in a return statement. Now, think about the first program I showed, what's it doing when it's done? Nothing. It prints to the screen and that's it. It shuts down and clears out memory. But what if I need the distance later? Well, that's where return comes in. It makes sure that after doing the calculations, before clearing out of memory, it wants to give it back to you.
If you've learned about Math.random()
, notice that you need to store or use it, otherwise it's gone for good. That's because Math.random()
has a return type of a double. Something like System.out.println()
has a return type of void because it doesn't 'return' anything, just displays text to our screen.
The basic premise behind a method is:
<access modifier> <return type> <name> (<parameters>) { }
The access modifier, for right now, should just stay public static
. You'll learn about classes later. The return type
is the important thing because this like like when you make a variable; you had to tell Java what data type it was - same for a method. You have to tell Java what data type this tiny, little program will produce. name
is no different than when you named variables but now, you have to add in parameters
, which are just placeholders in the method because we don't know what the values/variables that will be used later are going to be!
Now that I have distance
as a method, I can use it three times whereever I want:
double distAB = distance(4, 0, 4, 4);
double distAC = distance(4, 0, 4, 2);
double distBC = distance(4, 4, 4, 2);
if (distAB == distAC + distCB)
System.out.println("Collision Detected");