Prerequisites: Java Basics Part 1
Learn about the Java fundamentals and basics that a beginner needs to know before starting Burp Suite Extension development.
We'll cover the following...
Java is an object-oriented programming language. The core concept of the object-oriented approach is to break complex problems into smaller objects.
An object is any entity that has a state and behavior. For example, a car is an object. It has:
- States- idle, first gear, etc.
- Behaviours- braking, accelerating, etc.
Before we delve more deeply into objects, though, let’s first learn about classes in Java
Java Class
A class is a blueprint for the object. Before we create an object, we first need to define the class.
We can think of the class as a sketch, or prototype, of a house. It contains all the details of the floors, doors, windows, etc. Based on these descriptions, one can build the house which in this case is the object.
Just like many identical houses can be made from the same blueprint, many objects can be created from a class
Creating a class in Java
We can create a class in Java using the class keyword. For example:
class ClassName {
// fields
// methods
}
Here, fields (variables) and methods represent the state and behavior of the object respectively.
- fields are used to store data
- methods are used to perform some operations
For our car object, we can create the class as:
class Car {
// state or field
private int gear = 10;
// behavior or method
public void braking() {
System.out.println("Applying breaks");
}
}
In the above example, we created a class named Car. It contains a field named gear and a method named braking().
In this example, Car is a prototype. Now, we can create any number of cars using the prototype, all of which will share the fields and methods of the prototype.
Note: We have used keywords
privateandpublic. These are known as access modifiers.
Java Object
Continuing with our example of the Car class, sedan, coupe, hatchback etc. can all be considered objects of this class.
Creating an Object in Java
Here is how we can create an object of a class:
className object = new className();
// for Car class
Car sedan = new Car();
Car coupe = new Car();
We used the new keyword along with the constructor of the class to create an object. Constructors are similar to methods and have the same name as the class. For example, Car() is the constructor of the Car class.
Here, sedan and coupe are the names of objects. We can use them to access the fields and methods of the class.
Here, we have created two objects of the Car class. We can create as many objects as we want from a single class in Java.
Note: Fields and methods of a class are also called members of the class.
Access Members of a Class
We can use the name of objects along with the . operator to access members of a class. Try playing with this in code widget below.
In the above example, we have created a class named Car. It includes a field named gear and a method named braking(). Notice the statement,
Car sedan = new Car();
Here, we created an object of class Car named sedan. We then use the object to access the field and method of the class.
sedan.gear- access the field gearsedan.braking()- access the method braking()
To learn more, read about Java Constructors, try this espresso shot here
Check your knowledge!
A Java class can contain ___ .
Variables
Methods and Constructors
Inner Classes (A class inside another class)
All of the above