Defining Classes
Navigate Classes and Objects topic: ) |
[edit] Fundamentals
Every class in Java can be composed of the following elements
- fields or member variables — Fields are variables that hold data specific to each object. For example, an employee might have an ID number. There is one field for each object of a class.
- member methods — Member methods perform operations on an object. For example, an employee might have a method to issue his paycheck or to access his name.
- static fields — Static fields are common to any object of the same class. For example, a static field within the Employee class could keep track of the last ID number issued. Only one static field exists for one class.
- static methods — Static methods are methods that do not affect a specific object.
- inner classes — Sometimes it is useful to contain a class within another one if it is useless outside of the class or should not be accessed outside the class.
- Constructors — A special method that generates a new object.
- Parameterized types — Since 1.5, 'parameterized types' can be assigned to a class during definition. The 'parameterized types' will be substituted with the types specified at the class's instantiation. It is done by the compiler. It is similar to the C language macro '#define' statement, where a preprocessor evaluates the macros.
![]() |
public class Employee // This defines the Employee class.
{ // The public modifier indicates that // it can be accessed by any other class private static int nextID; // Define a static field. Only one copy of this will exist, // no matter how many Employees are created. private int myID; // Define fields that will be stored private String myName; // for each Employee. The private modifier indicates that // only code inside the Employee class can access it. public Employee(String name) // This is a constructor. You can pass a name to the constructor { // and it will give you a newly created Employee object. myName = name; myID = nextID; // Automatically assign an ID to the object nextID++; // Increment the ID counter } public String getName() // This is a member method that returns the { // Employee object's name. return myName; // Note how it can access the private field myName. } public int getID() // This is another member method. { return myID; } public static int getNextID() // This is a static method that returns the next ID { // that will be assigned if another Employee is created. return nextID; } } |
The following Java code
![]() |
public class EmployeeList {
public static void main(String[] args) { System.out.println(Employee.getNextID()); Employee a = new Employee("John Doe"); Employee b = new Employee("Jane Smith"); Employee c = new Employee("Sally Brown"); System.out.println(Employee.getNextID()); System.out.println(a.getID() + " : " + a.getName()); System.out.println(b.getID() + " : " + b.getName()); System.out.println(c.getID() + " : " + c.getName()); } } |
would produce this output:
0 3 0 : John Doe 1 : Jane Smith 2 : Sally Brown
[edit] Constructors
A constructor is very similar to a method in that it has remarkably similar syntax. There are, however, a number of very key differences. Notably, a constructor has the same name as the class, and has no return type. Be careful, though, since it is possible to create a method with the same name as the class, and is not a constructor! Example:
![]() |
public class Cheese {
//This is a constructor public Cheese() { doStuff(); } //This is a method with the same name as the class public void Cheese() { doStuff(); } } |
The previous syntax is correct, but it is always a bad idea to have a method with the same name as the class.
Constructors, like methods, can be overloaded and take parameters.
![]() |
public class Cheese {
//This is a constructor public Cheese() { doStuff(); } public Cheese(int weight) { doStuff(); } public Cheese(String type, int weight) { doStuff(); } } |
One interesting feature of constructors is that if you do not specify a constructor in your class, the compiler will create one for you. This default constructor, if written out would look like
![]() |
public class Cheese {
public Cheese() { super(); } } |
The super()
command calls the constructor of the superclass. Every constructor begins with a call to super()
. You don't have to worry about writing it out, usually, because the compiler will, once again, add it for you. That said, there are instances where you need to add in the call manually. For example, if you write even one constructor, no matter what parameters it takes, the compiler will not add a default constructor. The following code results in a runtime error:
![]() |
public class Cheese {
public Cheese(int weight, String type) { doStuff(); } } public class Mouse() { public void eatCheese() { Cheese c = new Cheese(); ///Oops, compile time error! } } |
This is something to keep in mind when extending existing classes. Either make a default constructor, or make sure every class that inherits your class uses the correct constructor