-1

I'm learning Java on Codecademy and recently completed a project that calculates monthly payments for a car loan. The problem is that I don't understand the solution, and no one has responded to my question about it on the Codecademy forum.

Why are the instance variables created in the main method scope instead of just after the class has been declared? We haven’t seen any examples of this prior to this project and I don’t understand.

Here is the code:

//Calculates monthly car payment
public class CarLoan {
//Why aren't the variables created here rather than in the main method?
  public static void main(String[] args) {

    int carLoan = 10000;
    int loanLength = 3;
    int interestRate = 5;
    int downPayment = 2000;

    if (loanLength <=0 || interestRate <=0) {
      System.out.println("Error! You must take out a valid car loan.");
  } else if (downPayment >= carLoan) {
      System.out.println("The car can be paid in full.");
  } else {
      int remainingBalance = carLoan - downPayment;
      int months = loanLength * 12;
      int monthlyBalance = remainingBalance / months;
      int interest = (monthlyBalance * interestRate) / 100;
      int monthlyPayment = monthlyBalance + interest;
      System.out.println(monthlyPayment);
    }
  }
}
smlisk0630
  • 15
  • 3
  • 3
    related https://stackoverflow.com/questions/2088299/what-is-the-difference-between-local-and-instance-variables-in-java – Scorpion May 21 '20 at 13:18
  • If the variables don't need to exist in a broader scope (that is, you don't need to keep their values after the method completes), they don't need to be declared outside the method. – Andy Turner May 21 '20 at 13:19
  • There is nothing preventing you from creating the variables there. That being said, it is usually a good practice to declare variables where they will be used. If they are only used in a method, then declare them in the method. – bcr666 May 21 '20 at 13:22
  • 3
    To be more explicit: Those are not *instance variables* (aka *fields*), they are *local variables*, and I find it very hard to believe that your Java training guide hasn't taught you about local variables (yet), if it is teaching you about instance variables. See e.g. [The Java™ Tutorials - Variables](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html). – Andreas May 21 '20 at 13:33
  • @Andreas The lesson on variables only discussed data types. The next lesson, object-oriented java, mentioned java variables inside a method, but it did not call them local variables or say anything else about them other than that they can't be used outside the scope of the method. Here's a summary sheet of the lesson: https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet – smlisk0630 May 21 '20 at 15:09
  • @smlisk0630 Then maybe you should find a **better** Java learning guide. – Andreas May 21 '20 at 17:27

1 Answers1

0

Variables defined within a method are local variables, they belong to an invocation of the method, not to an instance of an object.

The intent seems to be to provide an example that is understandable to beginners who haven't been introduced to constructors, instance variables, and methods. They want to teach local variable declaration, some simple calculating and if-statements, and printing to the console before getting into that other stuff.

As an exercise it would be fine for you to change the CarLoan class to give it instance variables, just to see another way to do it. Keep the variable values hard-coded, make an instance method that calculates the monthly payment, and have the main method print the result to the console.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Thanks for your help. I am only 39% of the way through the course, but it actually has introduced constructors, instance variables, and methods. It has not discussed local variables yet, at least by that name. – smlisk0630 May 21 '20 at 15:15
  • @smlisk0630: yes, they're not calling them local variables probably because they want to limit the amount of terminology they inflict on you. it looks as if the code here is designed to be understandable by someone who has covered only https://www.codecademy.com/learn/learn-java/modules/learn-java-variables and not the next lesson. – Nathan Hughes May 21 '20 at 15:18