Question about instance fields in Learn Java: Simple Car-payment Calculator in Conditionals and Control Flow

So I was working on the project: A Simple Car Loan Paymen Calculator. In Learn Java - conditionals and control flow

and in the get unstuck video i was wondering why all the instance fields such as:
int carLoan
int interestRate

why are those instance fields declared inside of main() as opposed to the outer scope of the class were using to build this project?

Here is my noobish thinking please enlighten me if im wrong! :slight_smile:

but my understanding is up to this point we use main() to actually depict what we want to run in our program. so if we wanted to create a reusable instance of an object we would define its class in the “outermost class” as i call it (sorry if thats wrong terminology lol).

My questions

so is the reason we write all the code in main in this project because we have the predetermined values that we want to use for the given example?

also would there be any concepts and circumstances where declaring all the instance fields inside of main as we do in this project be the most advantageous?

lastly - is this necessary because this is an if - statement? IE: with all if statements, do we need to write them in the main() method?

thanks all for input in advance!


Your question has to do with design principles.

The core of your program is a calculator. The actual loan amount and interest rate amount are factors that can change depending on context (either place or time). So any specific values for those are not really part of your core method.

Sometimes the core of your program could take read inputs from a csv file and calculate every row. Sometimes it just takes a few a values you input by hand.

In either case it’s generally the case that the input is not mixed with the core calculator method definition. The main method acts as a staging ground where this happens.

Please continue to ask questions if this is not clear.