Book Image

Learning Java by Building Android Games

By : John Horton
Book Image

Learning Java by Building Android Games

By: John Horton

Overview of this book

Table of Contents (17 chapters)
Learning Java by Building Android Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Our first class and first object


So what exactly is a class? A class is a bunch of code that can contain methods, variables, loops, and all other types of Java syntax. A class is part of a package and most packages will normally have multiple classes. Usually, but not always, each new class will be defined in its own .java code file with the same name as the class.

Once we have written a class, we can use it to make as many objects from it as we need. Remember, the class is the blueprint, and we make objects based on the blueprint. The house isn't the blueprint just as the object isn't the class; it is an object made from the class.

Here is the code for a class. We call it a class implementation:

public class Soldier {
  int health;
  String soldierType;

  void shootEnemy(){
    //bang bang
  }
  
}

The preceding snippet of code is a class implementation for a class called Soldier. There are two variables, an int variable called health and a string variable called soldierType.

There is also...