1. Fields
What is a class?
The word class is related to "classification","category", or "set". Classes in Java (and many other languages) are your own variable types.
Declaring a class
Typically, classes are defined in separate files. A ".java" file is only allowed one public class, although it can have multiple non-public classes. Additionally, classes declared inside of other classes are called inner classes. You cannot declare classes within functions or methods (the exception being anonymous classes).
See this code that declares a class Cat:
public class MyProgram { public static void main(String[] args) { Cat garfield = new Cat(); //This creates a new object of type Cat System.out.println(garfield); //You can print objects, which will print their memory address by default } } //This creates the new "Cat" variable type class Cat { //It is good style to capitalize classes, but this is not required by Java }
Instantiating Objects of a Class Type
While you might declare an integer with int x = 5
, classes are instantiated with the "new" operator. For example Cat x = new Cat();
creates a variable of type Cat with name x.
Fields
A class is like a variable type that contains other variables. The variables within an object are called "Fields". Here, we can add fields to the Cat type:
class Cat { int age; //Each Cat will contain an int, called age String name; //Each Cat will contain a String called name }
Accessing and modifying fields
Once a class has fields, you can modify and access the fields for each object you create:
public class MyProgram { public static void main(String[] args) { Cat mimi = new Cat(); mimi.name = "Mimi the fabulous"; //modifying a field mimi.age = 2; Cat garfield = new Cat(); garfield.name = "Garfield"; garfield.age = 42; System.out.println(mimi.name); //Prints "Mimi the fabulous" System.out.println(garfield.age); //Prints 42 } } //This creates the new "Cat" variable type class Cat { int age; //Each Cat will contain an int, called age String name; //Each Cat will contain a String called name }
Even though we declared int age and String name only once in the prior example, every cat has an age and a name. Mimi has an age and a name, and garfield has a different age and name. In other words, by creating the class Cat, we can create these name/page pairs every time we create a cat. We access the name inside of mimi with mimi.name
, which we can either modify or print.
Creating an array of Objects
Just like other variable types, you can create an array of class types. However, each index must be initialized using the "new ObjectName()" constructor before you can access its fields:
public class MyProgram { public static void main(String[] args) { Cat[] kitties = new Cat[3]; //An array that will hold 3 cats kitties[0] = new Cat(); kitties[0].name = "mimi"; kitties[0].age = 2; kitties[1] = new Cat(); kitties[1].name = "garfield"; kitties[1].age = 42; kitties[2] = new Cat(); kitties[2].name = "felix"; kitties[2].age = 2001; //we can now output all the kitties: for(int i = 0 ; i << kitties.length; i++) { //loop with i equaling 0,1, and 2 System.out.println(kitties[i].name+" is "+kitties[i].age+" months old"); } } } class Cat { int age; String name; }
Since kitties[0]
is a cat, we can access its fields with kitties[0].name
. As you can see, the for loop allows us to iterate over all cats in the array.