bock.steve
bock.steve

Reputation: 231

Java Syntax confusion, calling a variables method?

I have a class Ball, within it a variable velocity which is a Vector, declared as:

private Vector velocity;

Now somewhere else in the class, there is a function called bounce, declared as:

public void bounce(float surfaceTangent) {
    velocity = velocity.bounce(surfaceTangent);
}

I don't understand what that line assigning the velocity is doing, its unfamiliar syntax to me. It looks like its calling velocity's bounce function, but velocity is a variable, not a class. It doesn't have a function at all... What exactly is this doing?

Upvotes: 1

Views: 121

Answers (2)

NESPowerGlove
NESPowerGlove

Reputation: 5496

Velocity is an instance of class Vector (confusing name here because most people would associate that type name with java.util.Vector), which has instance methods and instance variables that belong to every instance of Vector. Bounce looks like an instance method here. In object oriented programming you usually interact with objects (instances) through their methods.

but velocity is a variable, not a class. It doesn't have a function at all...

In Java, classes do have their own class methods (and class variables), these are denoted by the method modifier of static.

Upvotes: 1

sol4me
sol4me

Reputation: 15698

You have the variable declared called velocity to which you are assigning value returned by the method bounce(float) invoked on the velocity instance.

Upvotes: 0

Related Questions