Results 1 to 3 of 3
  1. #1
    kashyapkeshav is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default needed clarificarions on constructors

    Hi am learning JAVA . in the below example

    1.how exactly the two samples differs please clarify

    Class A {
    int i=10;
    }

    and
    Class A {
    int i=10;
    A(intj);//Initializing the constructor
    }

    2. Whats the necessity of initializing the constructor when u are defining the variable .

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: needed clarificarions on constructors

    1. You second example is not correct, you might want something like this:

    Java Code:
    class A {
        int i = 10;
    
        A(int j) {
            // ...
        }
    }
    When you creating a class and doesn't provide a constructor it will have a default constructor. A constructor that doesn't have any parameter. A constructor is used to create an instance of corresponding class. The difference between your first and second example that your second example will not have the default constructor because you provided a constructor that take a single parameter. To instantiate your first class you can do it like A a = new A(); but the second A class can only be instantiate by A a = new A(0); You can't instantiate it using the default constructor.

    2. You use constructor to instantiate or initialize objects or variables in your class. This is to make sure that it is in the state that you want when you class it running it function.

  3. #3
    kashyapkeshav is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: needed clarificarions on constructors

    Thank you saryada ,

    i got a clear picture of it .

    thank you very much

Similar Threads

  1. Constructors
    By cups in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 11:55 AM
  2. Constructors
    By EdOBannon in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2011, 11:05 AM
  3. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  4. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

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