m.cekiera
m.cekiera

Reputation: 5385

Calling methods in Java

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.

Upvotes: 3

Views: 18928

Answers (4)

tsumnia
tsumnia

Reputation: 464

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");

Upvotes: 3

a_mid
a_mid

Reputation: 76

When you write this in a class in Java (or when it is implied), you are refering to the object instanciating that class so this is a reference to an object not a class. This is hard to get at first but there is a difference between the concept of object and class in object oriented programming and therefore in Java.

Note also that when you call a static method, you use the name of the class containing the static method because the static method is a concept defined for the class not the particular objects instanciating that class.

Upvotes: 1

Laerte
Laerte

Reputation: 7083

Basically, you can call non-static methods like this: method(); or this.method();

When you use the second syntax with this, you will be telling the compiler that you will be calling from the instance you are in (explicit).

Both will work the same way.

But, there is another example that can set this clear for you. Thinking on variables. Check this code:

private int a = 1;

public void method(int a) {
    this.a = a;
}

The object's variable is set using a local variable. If you do not use this for that call, the compiler would understand that the variable you are dealing with was just the local one.

Hope it helps.

Upvotes: 2

spudone
spudone

Reputation: 1277

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");
    }
}

Upvotes: 5

Related Questions