Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default instance fields?

    Hi

    I just started learning java. I found this code in one of the books. I don't understand how can a code run effectively without initializing balance in the first place.

    as you can see, private double balance is initialized in final line. so without initializing it, how can compiler know if its reading top to bottom left to right ? without knowing what is balance, how can compiler interpret balance = 0?????also, in the main method, there was no indication of balance as well..

    Java Code:
    /**
       A bank account has a balance that can be changed by 
       deposits and withdrawals.
    */
    public class BankAccount
    {  
       /**
          Constructs a bank account with a zero balance.
       */
       public BankAccount()
       {   
          balance = 0;
       }
    
       /**
          Constructs a bank account with a given balance.
          @param initialBalance the initial balance
       */
       public BankAccount(double initialBalance)
       {   
          balance = initialBalance;
       }
    
       /**
          Deposits money into the bank account.
          @param amount the amount to deposit
       */
       public void deposit(double amount)
       {  
          double newBalance = balance + amount;
          balance = newBalance;
       }
    
       /**
          Withdraws money from the bank account.
          @param amount the amount to withdraw
       */
       public void withdraw(double amount)
       {   
          double newBalance = balance - amount;
          balance = newBalance;
       }
    
       /**
          Gets the current balance of the bank account.
          @return the current balance
       */
       public double getBalance()
       {   
          return balance;
       }
    
       private double balance;
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 04-03-2010 at 05:00 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    balance is initialized in the class's constructor here:
    Java Code:
       public BankAccount()
       {   
          balance = 0;
       }
    But even without the constructor, all fields declared at the class level are initialized with default values which for a double is 0.0, an int is 0, and a reference variable, null.

    I believe that the order of code calling is that the class's declarations are called first
    Java Code:
    private double balance;
    then the constructor is called next.

    edit: as an aside, please note that I added code tags to your original post. To learn to do this yourself, please see the link in my signature below.

    Oh, and welcome to the forum!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by search4survival View Post
    Hi

    I just started learning java. I found this code in one of the books. I don't understand how can a code run effectively without initializing balance in the first place.

    as you can see, private double balance is initialized in final line. so without initializing it, how can compiler know if its reading top to bottom left to right ? without knowing what is balance, how can compiler interpret balance = 0?????also, in the main method, there was no indication of balance as well..
    The compiler indeed reads a source text once, line by line from top to bottom. It builds an internal structure given that source text, named an AST (Abstract Syntax Tree) and then it performs additional processing on that AST before it draws its conclusions. After building its AST it can see that 'balance' is declared and goes on with its code generation.

    About the initialization: if you don't initialize an instance variable it will be initialized implicitly to all bits zero which is any form of 0 (zero) or null. The compiler doesn't know anything about those values because the variables will only be initialized at runtime. The compiler only generates the instructions to manipulate those variables. If anything goes wrong during runtime the error will show up then (e.g. division by zero or a null value dereference).

    There's one small thing: explicit initialization happens from the top to the bottom, before that all variables are imlicitly initialized to zero (see above). So the next code snippet does funny things:

    Java Code:
    // at the class level (not in a method):
    int x= y+1; // y is still 0, so x == 1 now
    int y= x+1; // y == 1+1 now
    kind regards,

    Jos
    kind regards,

    Jos

  4. #4
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

    thanks jos......... :)
    Last edited by search4survival; 04-03-2010 at 05:21 PM. Reason: sorted

Similar Threads

  1. Searching in several fields
    By ivaneggel in forum Lucene
    Replies: 0
    Last Post: 11-11-2009, 09:24 AM
  2. Static fields
    By Eranga in forum Advanced Java
    Replies: 25
    Last Post: 08-29-2009, 11:06 AM
  3. Problem With Text Fields!
    By freshoreo in forum AWT / Swing
    Replies: 3
    Last Post: 08-04-2008, 09:52 PM
  4. Demonstration of text fields in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:20 PM
  5. Jsp / Java cannot get fields
    By Dracos in forum New To Java
    Replies: 0
    Last Post: 02-04-2008, 09:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •