Classes
A class is Java’s fundamental unit of storing code. Everything you write in Java will be inside of some kind of class (or similar structure).
A class in java is used to represent something, similarly to how an int
represents a number and a String
represents a string of text. A class may be used to represent an animal, a game piece, or even a robot.
You’ve probably seen this line at least once, at the top of your main file:
public class Main { [...] }
Don’t worry about what `public`` means for now, we’ll cover it more in depth in the page on packages.
The above statement defines a class called Main, with the code between the opening {
and closing }
parenthesis being considered a part of that class.
Class names must be unique within a package (folder), and are typically capitalized.
A class can contain three things:
- Methods (Functions).
- instance variables.
- Special methods called constructors.
Instance Variables
Instance variables are variables that are located within a given class. They differ from the type of variables that we have covered previously in that they are declared outside functions (unlike all the code we’ve seen before)
You may or may not have learned the difference between a variable declaration and a variable initialization.
Declaration is when you create a variable, but do not immediately assign it a value:
int x;
Initialization is when you assign the variable a value. In Java you are not allowed to access or change a variable before initializing it
Here’s an example of variable initialization:
x = 10;
Instance variables are variables that are declared at the top of a class, right after the class declaration. For example:
public class Animal {
public String name;
public int age;
public Animal child;
...
}
Instance variables in java should almost always be
private
.
Instance variables will be initialized later, in the constructor, which will be explained later.
Methods
Methods are the name given to functions in Java.
Methods == Functions
Any method within a class can access the instance variables of that class.
We’ll talk more about methods later.
Constructors
The Constructor of a class is what is used to create an instance of a class.
If you don’t know what an instance is, don’t worry about it. It will be explained in the page on Objects
They have no return type, and instead of a name, they use the name of the class.
Let’s look at an example of a constructor:
public class Animal {
private String name;
private int age;
private Animal child;
public Animal(int a, String n) {
//initialize instance variables
age = a;
name = n;
}
}
You can have multiple constructors with the same name in a class, as long as they have different signatures
Let’s look at an example of a class that has multiple constructors
public class Animal {
public String name;
public int age;
public Animal child;
public Animal(int a, String n) {
//initialize instance variables
age = a;
name = n;
}
public Animal(String n) {
age = 0;
name = n;
}
}
In the above example we can have multiple constructors due to the properties of Method Overloading
This is often used when you would like some instance variables to contain certain default values. (for instance, an animal’s age starting at 0
)
If you don’t need a constructor to do anything, you don’t have to write one. All classes in java have an implicit, or implied, constructor with no arguments which does nothing. We’ll cover how to call the constructors we make (or ones that are implied) next
Private and Public
You may have noticed two keywords before method or variable declarations: private
and public
. These are two of four access modifiers available in Java.
This may make more sense of you read the next section about objects first.
You will likely only use
public
andprivate
Syntax
Access modifiers are placed at the beginning of variable or method declarations like this:
//Variable
private int num;
//Method
public static void main(String[] args){}
In java, instance variables are typically private.
Different Access Modifiers
-
public
- a public method or field is directly accessible outside of the class it is defined in by any part of your program
-
private
- a private method or field is only accessible within the class where it is defined.
-
default
- a method or field with the default access modifier is only accessible within the package (often folder) of the class where it is defined.
-
protected
- a protected method or field is accessible only within the package of the class where it is defined, or within any child class of the class where it is defined.
Don’t worry if you don’t know what a child class or package is, it hasn’t been explained. You won’t really use default or protected in robotics.