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 8 of 8

Thread: Someone help me understand construstors?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Someone help me understand construstors?

    Hello All

    I cannot express how happy I am to have found a platform to ask all my Java related questions and there is many.

    first off I am complete beginner in the software development/programming field and have decided to start with Java as it can be used on any system and it seems that Java developers are in huge demand.

    My first question is can someone explain constructors to me?

    I keep re reading the same section of the book and cannot get my head around it? Can someone explain and give me an easy to follow example?


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Someone help me understand construstors?

    Quote Originally Posted by dowd View Post
    My first question is can someone explain constructors to me?
    In few words: a constructor is a special member of a class that serves to construct and initialize the state of a newly created object.
    Every class has always at least 1 constructor. If the programmer doesn't define at least 1 constructor, the compiler puts in a "default" constructor.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    GregBrannon (December 9th, 2013)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Someone help me understand construstors?

    Ok so I think I get waht your saying could you give me an example?

    How and why would I use it?

  5. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Someone help me understand construstors?

    Quote Originally Posted by dowd View Post
    Ok so I think I get waht your saying could you give me an example?

    How and why would I use it?
    public class Book {
        private String title;
     
        public Book(String title) {     // constructor
            this.title = title;
        }
    }
    This Book class has only 1 explicit constructor. It receives a book title and initializes the title field.

    So, for example:
    Book b1 = new Book("Java I/O, 2nd Edition");
    Book b2 = new Book("Effective Java, 2nd Edition");

    Each book object has its own title field initialized with the specified title string.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Someone help me understand construstors?

    ok in a essenses a constructors allows you initalise a variable with different values?

    public class User {
    private String Name;

    public User(String name) { // constructor
    this.name = name;
    }
    }

    So you would have:

    User User1 = new user (Bob Smiith)
    User User2 = new user (Tom Smith)

    I am getting this right?

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Someone help me understand construstors?

    Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

  8. #7
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Someone help me understand construstors?

    Quote Originally Posted by dowd View Post
    ok in a essenses a constructors allows you initalise a variable with different values?

    I am getting this right?
    Yes, in general a constructor serves at least for 2 things: to call, implicitly or explicitly, the "super" constructor (the constructor of a superclass) and eventually (this is up to you, and depending on your class) to initialize the "state" (fields) of objects of your class.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  9. The Following User Says Thank You to andbin For This Useful Post:

    dowd (December 9th, 2013)

  10. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Someone help me understand construstors?

    Excellent that kind of makes sense now thanks alot.

Similar Threads

  1. Please Help me to understand
    By Theillusion in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 17th, 2013, 03:09 AM
  2. Please help to Understand x=++x+ + +x+x++
    By rayan2004 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 2nd, 2012, 07:47 PM
  3. Can someone help me understand toString()?
    By Psychotron in forum Java Theory & Questions
    Replies: 10
    Last Post: January 18th, 2012, 10:43 PM
  4. Help me to understand this
    By Madhushan in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2011, 08:47 AM
  5. I dont understand why this happens....
    By ashenwolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 09:31 PM