Query Ledger Data
Summary
In this section, we will guide you through the steps of querying your account balance, enabling you to retrieve the most current information about the available funds in your new Hedera account.
Prerequisites:
Query the account balance
Get the cost of requesting the query
You can request the cost of a query prior to submitting the query to the Hedera network. Checking an account balance is free of charge today. You can verify that by the method below.
//Request the cost of the query
Hbar queryCost = new AccountBalanceQuery()
.setAccountId(newAccountId)
.getCost(client);
System.out.println("The cost of this query is: " +queryCost);
Get the account balance
You will verify the account balance was updated for the new account by requesting a get account balance query. The current account balance should be the sum of the initial balance (1,000 tinybars) plus the transfer amount (1,000 tinybars) and equal to 2,000 tinybars.
//Check the new account's balance
AccountBalance accountBalanceNew = new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client);
System.out.println("The new account balance is: " +accountBalanceNew.hbars);
⭐ Congratulations! You have successfully transferred _HBAR_** to another account on the Hedera Testnet! If you have followed the tutorial from the beginning, you have completed the following thus far:**
Set up your Hedera environment to submit transactions and queries.
Created an account.
Transferred HBAR to another account.
Do you want to keep learning? Visit our the SDKs & APIs section to take your learning experience to the next level. You can also find additional Java SDK examples here.
Code Check ✅
Your complete code file should look something like this:
Sample output:
New account ID: 0.0.13724748
New account balance: 1000 tinybars.
The transfer transaction from my account to the new account was: SUCCESS
The cost of query: 0 tℏ
The account balance after the transfer: 2000 tinybars.
Last updated