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
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.
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
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
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.
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();
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
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?
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
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.
Re: does a java class need a constuctor?