12 min readβ’june 18, 2024
Kashvi Panjolia
class
keyword, and name the class. Then, you add a set of curly braces {}
that will contain the contents of the class. To understand the key components of classes, we will create an AreaCalculator
class. This is the class header:public class AreaCalculator {}
AreaCalculator
class, we can create an instance variable to store the number of shapes a user has asked to calculate the area for:public class AreaCalculator {
private int numShapes;
}
AreaCalculator
.numShapes
, but we haven't initialized it yet. To initialize instance variables, we need to create a constructor. A constructor in Java is a special method that is used to create and initialize an object of that class. Every class must have at least one constructor, and you can define additional constructors as well. If you do not define a constructor for your class, a default one will be created.numShapes
instance variable.public class AreaCalculator {
private int numShapes;
public AreaCalculator() {
numShapes = 0;
}
}
public class Car {
private String brand;
private String model;
private int year;
public Car (String make, String carModel, String yearMade) {
brand = make;
model = carModel;
year = yearMade;
}
}
// Using these two slashes will create a single-line comment.
/* Using the slash and an asterisk
will create a multi-line comment
for longer explanations. Be sure to
close it with an asterisk, then a slash. */
/** Using the slash and two asterisks will create a
Java API documentation comment. In these comments,
you can use tags to specify the parameters of a method
and the return values of the method. These comments aren't as important
for the AP CSA curriculum.
@param -- explanation of parameter
@return -- explanation of what the method returns */
public class AreaCalculator {
private int numShapes;
// the constructor initializes the numShapes instance variable to 0
public AreaCalculator() {
numShapes = 0;
}
}
numShapes
instance variable in our AreaCalculator
class:public int getNumShapes() {
return numShapes;
}
public void setYear(int newYear) {
year = newYear;
}
Car
object, we can use the setYear
method to update it.public double triangleArea(double base, double height) {
double area = (base * height) / 2;
numShapes++;
return area;
}
numShapes
by 1 to show that a user asked the AreaCalculator
to find the area of a shape. Try writing the rectangleArea
and trapezoidArea
methods on your own, then check below for the answers.public double rectangleArea(double length, double width) {
double area = length * width;
numShapes++;
return area;
}
public double trapezoidArea(double base1, double base2, double height) {
double area = (base1 + base2) * height / 2;
numShapes++;
return area;
}
AreaCalculator
class were created to be used by objects of that class. However, we don't really need to create an AreaCalculator
object to be able to use the class since there is no object-specific information we are storing. We can create static variables and static methods that work across all objects of the class, but we don't need to create an object to access them. This is the AreaCalculator
class when made static:public class AreaCalculator {
private static numShapes;
// the constructor initializes the numShapes static variable to 0
public AreaCalculator() {
numShapes = 0;
}
// returns the numShapes static variable
public static int getNumShapes() {
return numShapes;
}
// calculates the area of a rectangle using the length and width
public static double rectangleArea(double length, double width) {
double area = length * width;
numShapes++;
return area;
}
// calculates the area of a triangle using the base and height
public static double triangleArea(double base, double height) {
double area = (base * height) / 2;
numShapes++;
return area;
}
// calculates the area of a trapezoid using the two bases and the height
public static double trapezoidArea(double base1, double base2, double height) {
double area = (base1 + base2) * height / 2;
numShapes++;
return area;
}
}
AreaCalculator.triangleArea(5.0, 3.0);
AreaCalculator
class where we computed the areas of the shapes, the same variable name area
was used to store the area of the shape. Why would the computer not throw an error for using the same name? Because of the scope of the variables.area
variables were all created inside their respective methods, which meant they only existed inside those methods, and were deleted by the computer once the method had ended. Therefore, the three area
variables never existed at the same time, and we were able to use the same name for all of them.numShapes
variable had a global scope because we were able to use it in all of the methods of the AreaCalculator
class.area
variable had a local scope because it was defined each method.this
keyword refers to the current instance of an object. It is used to access the fields and methods of the current object from within the object's own methods.this
keyword can be used in a Java class:public class MyClass {
int x;
public void setX(int x) {
this.x = x; // "this" refers to the current object
}
}
setX
method sets the value of the x
field for the current object. The this.x
notation is used to specify that the x
field of the current object should be modified, rather than a local variable named x
that might be defined within the method.this
keyword is also used to pass the current object as an argument to a method. For example:public class MyClass {
int x;
public void setX(int x) {
this.x = x;
}
public void updateX(MyClass obj) {
obj.setX(this.x); // "this" is used to pass the current object as an argument
}
}
updateX
method calls the setX
method on another object (obj
), passing the value of the x
field for the current object as an argument. The this.x
notation is used to specify the value of the x
field for the current object, and the this
keyword is used to pass the current object of MyClass
as an argument to the setX
method.this
keyword is a useful tool for disambiguating between fields and local variables with the same name, and for passing the current object as an argument to a method.Β© 2024 Fiveable Inc. All rights reserved.