-1

I have a quick question. I know how to use the keyword this in Java with a constructor that has parameters/arguments. Can you use this with a default constructor that has no parameters/arguments?

Example with code Class BankAccount below.

We created a method within this class to withdraw as much as possible. Within the method, I created a new BankAccount object to test with the tests provided by the Professor. Instead of creating theaccount object, he wanted me to use this. Is this possible without a constructor that holds parameters/arguments?

public double getOrAsMuchAsPossible(double requestAmount) throws InvalidAmountException, InsufficientFundsException
    {
        //Declare and initialize the variable amount to be used with in the method
        double amount = 0;
        //Create a new BankAccount object account
        BankAccount account = new BankAccount();
        //Deposit money into the account
        account.deposit(400);

        //Try to get requestAmount
        try
        {
            //Set the amount to the request amount and withdraw from account
            amount = requestAmount;
            account.withdraw(requestAmount);
        }
        //Catch the exception with the InsufficientFundsException
        catch(InsufficientFundsException exception)
        {
            System.out.println("Withdrawing amount: " + amount +  " that is larger than balance: " + balance + " is not allowed");
        }
        //If the account balance is less than the amount requested
        if(account.balance<requestAmount)
        {
            //The amount will equal the account balance, withdraw the amount from the account
            amount = account.getBalance();
            account.withdraw(amount);
        
        }
        return amount;
   }
user207421
  • 305,947
  • 44
  • 307
  • 483
PresFelber
  • 61
  • 6
  • 1
    I don't get your question. you can use `this` (meaning this current instance) in _any_ instance methods... – Eugene Mar 22 '21 at 03:25
  • 1
    What happened when you did what the professor suggested? If you haven't tried it, why not? That said, it seems you don't fully grasp the use of `this` quite yet. – MarsAtomic Mar 22 '21 at 03:26
  • this will help [Can I call methods in constructor in Java?](https://stackoverflow.com/questions/5230565/can-i-call-methods-in-constructor-in-java) – deadshot Mar 22 '21 at 03:28
  • In a constructor, this can only be used after `super()` implicitly or explicitly if I recall correctly. – Richard Barker Mar 22 '21 at 03:45
  • I think by "use `this`", the prof means for you to have your method do stuff **only on this** `BankAccount` (i.e., the `BankAccount` instance on which the `getOrAsMuchAsPossible` method is being called), not on some other totally different `BankAccount` that you've created for some special purpose. – Kevin Anderson Mar 22 '21 at 03:55
  • Thank you all for your quick feedback. I do understand the use of this, as we have been using it throughout class BUT we have always used it when creating an object with specific parameters, as well as in the getters and setters. This is the exact feedback from my professor...."Why create a new account?? Use the one your are in, aka. this." Which I took as not needing to create a new BankAccount object within the method. – PresFelber Mar 22 '21 at 04:49

2 Answers2

2

The java keyword "this" has no special interaction with constructors. It is often used in constructors to distinguish between parameter names and the newly created object's fields.

Something like

public class BankAccount {
    private int accountNum;

    public BankAccount() {
      this.accountNum = 4;
    }
}

Is perfectly valid, but redundant.

The main value to the "this" keyword in java is to access a field in a higher scope that has been masked in the current scope.

Classic Setter example

public void setAccountNum(int accountNum) {
    this.accountNum = accountNum;
}

In this case all references to accountNum would refer to the parameter. Use of the "this" keyword allows us to specify that it is the object's field called accountNum that is to be assigned a value.

Novabomb
  • 21
  • 1
0

Firstly, what is "this" keyword in java?

From oracle java docs:

Using the this Keyword within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

So coming to your question, your constructor need not to be parameterized to use this. You can use this keyword with default constructor as well.

Basically you only need to remember "The this keyword refers to the current object in a method or constructor."

Brooklyn99
  • 987
  • 13
  • 24