Results 1 to 8 of 8
  1. #1
    MIA6 is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default 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.

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

    Default

    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:
    Java Code:
    BankAccount myAccount = new BankAccount(); // default parameter-less constructor
    // call setters
    myAccount.setAcctNum("12345");
    myAccount.setBal(4000.00);
    myAccount.setOwner("Joe Smith);
    You could do it all in one constructor call:
    Java Code:
    BankAccount myAccount = new BankAccount("12345", 4000.00, "Joe Smith"); 
    // no setter calls needed
    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!

  3. #3
    MIA6 is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    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?

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

    Default

    Quote Originally Posted by MIA6 View Post
    thanks. but sometimes instance fields initialized some values already, then by calling constructor, paramters will change/substitute their initial values?
    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.

    and what do you mean by instance? you mean object?
    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 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?
    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 what's the difference btw instance field and instance variable?
    As far as I know, nothing; they are one and the same.

  5. #5
    MIA6 is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    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)

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

    Default

    Quote Originally Posted by MIA6 View Post
    ok, and I realized that some classes have constructors and some don't. So is constructor required or not?
    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.

    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?
    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.

    (Sorry for not using code, don't know how)
    Have you tried to click on the link in my signature and read how to do it?
    Last edited by Fubarable; 11-02-2009 at 06:42 AM.

  7. #7
    MIA6 is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    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.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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

  1. What is Instance, Empty Constructor ....
    By tking88 in forum New To Java
    Replies: 21
    Last Post: 10-26-2009, 02:38 PM
  2. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  4. New Instance for SWT
    By srinivasa_v in forum SWT / JFace
    Replies: 1
    Last Post: 08-08-2007, 01:02 AM

Posting Permissions

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