Results 1 to 8 of 8
Thread: Constructor and instance field
- 11-02-2009, 04:55 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Constructor and instance field
Hi, Im really confused about what is a constructor and its relation to 'instance field'. Is instance field the same as declaring variable except it has scope indicator like private or public? For example,
private String acctnum = "";
private double balance = 0.00;
private String owner = "";
//constructor
public BankAccount(String anum, double bal, String o)
{
acctnum = anum;
balance = bal;
owner = o;
}
So the top is instance field which describes the object's attributes, but I don't understand the stuff in parameter in the constructor; why you have to make them equal to the variables in instance field? and what does a constructor do? and sometimes within a method, you can also declare variables, but why didn't you put them in the instance field in the first place? Thanks.
-
Instance fields are non-static fields that have the whole class as their scope -- they are visible in all instance methods and constructors of the entire class. And yes, they are attributes and define the "state" of the object.
By having a constructor with parameters as you show above, you can both create a new object by calling this constructor, and set the state of the instance fields to the values passed by the parameters all at the same time. So instead of doing:
You could do it all in one constructor call:Java Code:BankAccount myAccount = new BankAccount(); // default parameter-less constructor // call setters myAccount.setAcctNum("12345"); myAccount.setBal(4000.00); myAccount.setOwner("Joe Smith);
As an aside, please look at my signature to learn to use code tags when posting code. Thanks, much luck and welcome to the forum!Java Code:BankAccount myAccount = new BankAccount("12345", 4000.00, "Joe Smith"); // no setter calls needed
- 11-02-2009, 05:26 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
thanks. but sometimes instance fields initialized some values already, then by calling constructor, paramters will change/substitute their initial values? and what do you mean by instance? you mean object? and I don't really get the concept of creating a new object. Here is what i think-- coz we already have a blueprint of the object, so by using the main method, we actually make the object come to existence which is creating a new object? And what's the difference btw instance field and instance variable?
-
Yep, sometimes we initialize the instance fields at declaration, usually if we want to give them default values that may or may not be changed.
Yes, I do. In my mind an instance is an object and visa versa. Please be forewarned though that I've not had formal Java training and may not be fully versed on the subtleties of this.and what do you mean by instance? you mean object?
That's a good way to start thinking about objects, but realize that they can be instantiated anywhere, not just in the main method.and I don't really get the concept of creating a new object. Here is what i think-- coz we already have a blueprint of the object, so by using the main method, we actually make the object come to existence which is creating a new object?
As far as I know, nothing; they are one and the same.And what's the difference btw instance field and instance variable?
- 11-02-2009, 06:30 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
ok, and I realized that some classes have constructors and some don't. So is constructor required or not? For example,
class Bicycle {
int speed = 0;
int gear = 1;
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
....
}
So why in this case, it doesn't have a constructor.
and also, sometimes in the parameter of the constructor, it doesn't include every variable that has been declared earlier. Example would be:
{ public String username;
private String password;
public Player (String name)
{ username = name;
}
public void setPassword(String passwrd)
{ password = passwrd;
}
In this case, constructor's parameter only includes username, and another method includes password, why? (Sorry for not using code, don't know how)
-
Yes, every class must have a constructor, but, if you don't explicitly add one to the class, Java will create a default constructor that takes no parameters and implicitly add it to your class.
coder preference? Some variables are more fixed than others. The ones that usually won't change may not need to be set by parameters. Also note that a class can have many constructors, each taking a different group of parameters.and also, sometimes in the parameter of the constructor, it doesn't include every variable that has been declared earlier. ...
// code...
In this case, constructor's parameter only includes username, and another method includes password, why?
Have you tried to click on the link in my signature and read how to do it?(Sorry for not using code, don't know how)Last edited by Fubarable; 11-02-2009 at 06:42 AM.
- 11-02-2009, 04:27 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
You said “The ones that usually won't change may not need to be set by parameters.” I'm again confused here, so what's really the point of having a constructor? just to set some values? but we can do the same in methods, like the example (password) I showed you. why some variables are declared in the instance field, but some are declared within methods? (are these called 'local variables'?)
Last edited by MIA6; 11-02-2009 at 04:52 PM.
- 11-02-2009, 06:44 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Instance variables are part of the state of the object (if they are not transient). They live (on the heap) with the object and die with the object. Method local variables are temporary variables used to help achieve the goal of the methods. They only live (on the stack) as long as the method is running.
Similar Threads
-
What is Instance, Empty Constructor ....
By tking88 in forum New To JavaReplies: 21Last Post: 10-26-2009, 02:38 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
New Instance for SWT
By srinivasa_v in forum SWT / JFaceReplies: 1Last Post: 08-08-2007, 01:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks