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: which class has a default constructor? (Req. guidance on constructor)

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

    Exclamation which class has a default constructor? (Req. guidance on constructor)

    Given:

    class X {}
    class Y {Y() {}}
    class Z {z(int i) {}}

    which class has a default constructor?

    Is it true? That class X and Z have default constructor? or X, Y and Z all three have default constructor?

    As constructor have same name in same case as class name, with no return type and may or may not have one or many parameters.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: which class has a default constructor? (Req. guidance on constructor)

    Classes are automatically given default constructors if no constructor is defined. So, class X will be given a default constructor, while class Z will not. Obviously, a parameterless constructor is provided in Y.

    •Default constructor. If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super()) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).
    •Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.
    Java: Constructors
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    DragBall (June 27th, 2012)

Similar Threads

  1. Constructor
    By kbbaloch in forum Object Oriented Programming
    Replies: 6
    Last Post: February 1st, 2012, 11:40 PM
  2. Constructor help
    By sambar89 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 26th, 2011, 11:17 PM
  3. Trouble using enum in constructor when creating a class
    By willmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 13th, 2011, 10:48 AM
  4. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 09:51 PM
  5. Construct a class that implement ActionListener with no constructor
    By striko_514 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 5th, 2010, 03:15 PM

Tags for this Thread