|
-
Sep 19th, 2006, 10:01 AM
#1
Thread Starter
Frenzied Member
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
-
Sep 19th, 2006, 12:43 PM
#2
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.
-
Sep 19th, 2006, 03:54 PM
#3
Thread Starter
Frenzied Member
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.
-
Sep 20th, 2006, 10:33 AM
#4
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
-
Sep 20th, 2006, 11:17 AM
#5
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.
-
Sep 20th, 2006, 11:59 AM
#6
Thread Starter
Frenzied Member
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();
-
Sep 20th, 2006, 12:15 PM
#7
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
-
Sep 20th, 2006, 12:42 PM
#8
Thread Starter
Frenzied Member
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.
-
Sep 20th, 2006, 01:01 PM
#9
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
-
Sep 20th, 2006, 01:16 PM
#10
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.
-
Sep 20th, 2006, 03:37 PM
#11
Thread Starter
Frenzied Member
Re: does a java class need a constuctor?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|