Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why use a constructor in intialize and not Setter

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why use a constructor in intialize and not Setter

    I just took beginning java oop and what I don't understand is why use a no-arg constructor to initialize a variable instead why not use a setter method(setLength of field private variable). I can understand using a constructor that sends arguments to initilize when you create a new object from a program. Does the variable have to be initialized?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,317
    My Mood
    Mellow
    Thanks
    253
    Thanked 780 Times in 765 Posts

    Default Re: Why use a constructor in intialize and not Setter

    One can use either approach, neither is mandatory. If an instance variable must have a specific value dictated by the state of the caller when the instance is created, passing that value to the constructor makes perfect sense. Otherwise, instance variables can be left to default values or initialized to specific values other than defaults in the no-argument constructor. Any mix of these possible approaches is acceptable.

    Instance variables are initialized by default according to specific rules that you can find online. Local variables are not initialized by default and must be initialized by the program.

Similar Threads

  1. setter of Type Enum
    By vector_ever in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 9th, 2013, 04:33 PM
  2. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 05:42 PM
  3. Needing some help with array getter and setter methods!!
    By BlackShadow in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 5th, 2012, 08:11 PM
  4. Constructor help
    By sambar89 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 26th, 2011, 11:17 PM
  5. [SOLVED] Project help...confusion with changing a getter/setter for a driver class
    By coolidge in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2011, 12:08 AM