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

Thread: Constructor

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

    Default Constructor

    Hi
    can any one tell me what is functionality of constructor....................?


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Constructor

    Quote Originally Posted by kbbaloch View Post
    Hi
    can any one tell me what is functionality of constructor....................?
    Did you even try to google?

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Constructor

    It is code that is executed when you create an object.

  4. #4
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Constructor

    As the first responder asked you, have you tried google before posting it here? I think most of us get tempted to post questions even though they might be within reach (answers). Nevertheless, I will answer that for you: A constructor is like a method but it MUST have the same name as the class and it is used to initialize the members of that class. It can have parameters or be default - without parameters. Consider:
    public class Person
    {
    int weight;
    String name;
    // constructor
    public person(int wght, String nm)
    {
    weight = wght; // you can also use the (this) keyword here
    name = nm // you can also use the (this) keyword here to refer to these members
    }
    } // end
    I hope this will help you.
    Last edited by helloworld922; January 31st, 2012 at 11:52 AM.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Constructor

    Quote Originally Posted by elisha.java View Post
    As the first responder asked you, have you tried google before posting it here? I think most of us get tempted to post questions even though they might be within reach (answers). Nevertheless, I will answer that for you: A constructor is like a method but it MUST have the same name as the class and it is used to initialize the members of that class. It can have parameters or be default - without parameters. Consider:
    [highlight = java]
    public class Person
    {
    int weight;
    String name;
    // constructor
    public person(int wght, String nm)
    {
    weight = wght; // you can also use the (this) keyword here
    name = nm // you can also use the (this) keyword here to refer to these members
    }
    } // end
    [/highlight]
    I hope this will help you.
    You aren't helping the OP much (regardless of how simple the question is) by posting unformatted code. Nobody wants to read it. Put your code in code blocks please.

    In response:

    Like qurtan stated, a constructor is code that is run when the object is initialized. You can use it to set variables the Object needs at the start of its life or make the caller provide the object with vital information it needs to function.

    [CODE]
    public class MyObject {
     
        //Note the public keyword.  Constructor access can be modified just like a method.  Public, protected, private and default are all supported.
        //Also note that a constructor MUST have the same name as the class and compilation unit.  Otherwise it is invalid.
        public MyObject() {
            //Initialization code here
        }
     
        //Note the difference between a constructor and a method.  Methods have return types (like void -aka- return nothing).
        public void sayHello() {
            System.out.println("Hello world");
        }
    }
     
    A constructor with parameters:
     
    public class Website {
     
        URL url;
     
        public Website(URL url) {
            this.url = url;
        }
    }
    [/CODE]

    Constructors are also inherited by superclasses, and you can call upon the superclass constructor at the beginning of the subclass constructor using the super keyword.
    Last edited by bgroenks96; December 29th, 2011 at 10:50 PM. Reason: Added highlights

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructor

    Thanx fot this great info. I was needy about this.. Thanks you have solved my problems

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Constructor

    Just to add some rules which will apply to constructors other then these constructor is just a normal method.

    Constructor Rules

    A constructor can't have a return type.
    Constructor must have the same name as class name.
    Constructors can't be marked static
    Constructor can't be marked Final or abstract

    Note: Constructor can't be overridden.

    Quoted from URL

Similar Threads

  1. Constructor help
    By sambar89 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 26th, 2011, 11:17 PM
  2. constructor X is undefined
    By ober0330 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 24th, 2011, 01:51 PM
  3. [SOLVED] Error in constructor
    By hello_world in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 14th, 2011, 07:46 PM
  4. [SOLVED] Cannot Find Constructor
    By jrookie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 8th, 2010, 02:54 PM
  5. Point constructor
    By upad in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 5th, 2010, 07:34 PM