Results 1 to 11 of 11

Thread: does a java class need a constuctor?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    does a java class need a constuctor?

    does a java class need a constructor, in this code there does not seem to be any constructor?

    Code:
    class CheckingAccount {
    
       private double balance = 0;
       public void setBalance(double bal) {
          balance = bal;
       };
    
       public double getBalance(){
    
          return balance;
       };
    }
    
    
    class Encapsulation {
    
       public static void main(String args[]) {
          System.out.println("Starting myEncapsulation...");
          CheckingAccount myAccount = new CheckingAccount();
          myAccount.setBalance(40.00);
          System.out.println("Balance = " + myAccount.getBalance());
       }
    }

    in the code line
    Code:
    checkingaccount myaccount = new checkingaccount();
    does the checkingaccount() call a function or does it call the class since there is a pari of brackets at the end of the code

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: does a java class need a constuctor?

    If no constructor at all is defined, the compiler will implicitly define one that takes no arguments and does nothing except inline initializations.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: does a java class need a constuctor?

    thanks for the reply

    what is an inline initialisation

    what about if no destructor is defined?

    in the code line
    Code:
    checkingaccount myaccount = new checkingaccount();
    does the checkingaccount() call a function or does it call the class since there is a pari of brackets at the end of the code
    Last edited by vb_student; Sep 19th, 2006 at 04:03 PM.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: does a java class need a constuctor?

    Again as CornedBee said, it'll call the no-arguments constructor, if you didn't create one, java will do that.
    In-line initialization is calling the constructor of the super class and allocating memory for the attributes and function

    EDIT: Java doesn't have a "destructor" you can override the "finalize()" method
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: does a java class need a constuctor?

    Actually, by in-line I meant stuff like this:
    Code:
    public class Whatever
    {
      int i = 100;
    }
    but what ComputerJy said applies too, of course. Except for the allocating memory stuff: memory for the attributes is already allocated by the time the constructor is called, and functions don't take memory.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: does a java class need a constuctor?

    thanks for the replies guys, it makes sense



    can i break the following code

    Code:
    checkingaccount myaccount = new checkingaccount();
    into

    Code:
    checkingaccount myaccount ;
    myaccount = new checkingaccount();

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: does a java class need a constuctor?

    If you try it and it works then it's ok to do so.
    Yes its ok to do it
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: does a java class need a constuctor?

    thanks for the reply

    the superclass is the base class, right?

    what attributes and methods does the superclass contain?

    in the code
    Code:
    checkingaccount myaccount = new checkingaccount();
    isnt't the constructir returning an address which is assigned to the object myaccount?

    or is it the keyword new which is returning the address?
    Last edited by vb_student; Sep 20th, 2006 at 12:59 PM.

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: does a java class need a constuctor?

    Depends on the super class you are deriving from, if it's nothing (class Object implicitly), read javadoc for more information
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: does a java class need a constuctor?

    The keyword new returns the reference (there's no such thing as raw addresses in Java). The constructor call initializes the object.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: does a java class need a constuctor?

    thanks guys
    makes sense

Posting Permissions

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



Click Here to Expand Forum to Full Width