Declared field in Java

anyone help?

package OOP.VN;

public class TaxReport {
// Please help to explain why declared like this instead of int, double… prefix

**private TaxCalculator2018 calculator;** // private doutble/int.. calculator

public TaxReport(){
    calculator = new TaxCalculator2018(10_000);
}
public void show(){
    var tax = calculator.calculateTax();
    System.out.println(tax);
}

int, double, and the other familiar names are the built-in data types in Java. TaxCalculator2018 is a custom class that someone created. Programs can use a mix of built-in data types and classes.

Here I’m saying that myNumber refers to an integer:

int myNumber;

And here I’m saying that myCalculator refers to an “instance” of the Calculator class:

Calculator myCalculator;

In both cases we are declaring a variable and the type of the variable. It’s just that in the first example we are using a built-in data type, and in the second example we are using a custom class.

This kind of stuff is a bit confusing at first but it might make more sense as you learn more about Java and class-based object-oriented programming.

1 Like