Classes contain methods
Explore how methods are grouped within Java classes and how non-static methods access object data through the this keyword. Learn to implement move and bounce methods in a Ball class to control object behavior and animation. Understand the difference between static and non-static methods with practical coding examples.
We'll cover the following...
Here is some code to create a custom Ball class that is used to store information about a ball that will bounce on the screen. The main method of BallExample creates a Ball object using the new keyword and a call to the constructor. Read the code carefully now:
The BallExample class also has a method drawBall that accesses instance variables of the Ball object and uses them, together with a Canvas object to draw a circle in the right place on the screen.
Classes group together methods. It seems like the code for drawBall might naturally be in the Ball class. Like this:
The BallExample class ...