Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling methods in Java

Tags:

java

methods

call

I'm a beginner and I'm trying to get a grasp on methods in Java. In general, I understand differences between static and non-static methods, but sometimes, reading others code, I'm confused about how a particular call is written.

As I understand, static method can be called with or without object. Non-static method need an object to be called, however when non-static method is called in another non-static method, in written form, it can be called just by a name(like method()), without a written reference to object (like object.method() or this.method()).

Is there another situation, when a non-static method call can be written this way? Is there another way to call a method beyond those?

I would be grateful for any comments.

like image 699
m.cekiera Avatar asked Jan 29 '26 03:01

m.cekiera


2 Answers

Maybe you're thinking of a call to a method within the same class, e.g.:

public class Foo {
    public void doSomething() {
        doSomethingElse();   // equivalent to "this.doSomethingElse()"
    }

    private void doSomethingElse() {
        System.out.println("Bar");
    }
}
like image 119
spudone Avatar answered Jan 30 '26 16:01

spudone


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");
like image 32
tsumnia Avatar answered Jan 30 '26 17:01

tsumnia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!