-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;
   }
6
  • 1
    I don't get your question. you can use this (meaning this current instance) in any instance methods... Commented Mar 22, 2021 at 3: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. Commented Mar 22, 2021 at 3:26
  • this will help Can I call methods in constructor in Java? Commented Mar 22, 2021 at 3:28
  • In a constructor, this can only be used after super() implicitly or explicitly if I recall correctly. Commented Mar 22, 2021 at 3: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. Commented Mar 22, 2021 at 3:55

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.