CrazyEngineers
  • fact about java

    sushant005

    Member

    Updated: Oct 22, 2024
    Views: 940
    I came to know that when we compile a program without any constructor then a default constructor or a blank constructor is created at the run-time itself by a compiler.But if we create any of the constructor then this complier is not going to create ant default constructor.

    To see that default constructor created by the compiler at the time of compilation is
    javap -<filename>
    Use the above command after compiling and interpreting the program.
    Those who know this is fine but those who do not know just type this command and see a default constructor is created itself by the compiler.
    You will see a default constructor is created before the main() method and those lines of codes are in assembly language.

    But i want know that why this default constructor is created at the time of compilation by the compiler only if we do not define any form of constructor in the class definition before the main() method ?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sushant005

    MemberSep 1, 2010

    There is a correction,

    after compiling the java program if we write javap -c <filename>
    the above code will give the idea about the constructor execution and before the main() method a default constructor is created by the compiler itself if no constructor is being defined by the user/programmer.
    And the code appear in the command prompt. will be in assembly code.
    In case of inheritance after executing the program write javap -c <class name> and the process of execution of program how a class is extended.
    Are you sure? This action cannot be undone.
    Cancel
  • anandkumarjha

    MemberSep 2, 2010

    hello sushant
    so far as i know this default constructor is being created during compilation of java so as to destroy all the values of previous program.Although i am not sure .let me search it out
    Are you sure? This action cannot be undone.
    Cancel
  • sushant005

    MemberSep 2, 2010

    @anandkumar
    please justify your answer/comment.

    After lots of thinking i reached to the point that to initialized the object an default constructor is create by the compiler if no constructor is defined by the user.
    But we know that we make main() method static if no no object is created but in this case a default constructor is created is created which automatically create and initialized an object which is used to access the members of class.

    I do not know up to which point i am correct please do me correct if i am wrong at any point.
    Are you sure? This action cannot be undone.
    Cancel
  • Leo

    MemberSep 2, 2010

    Hey buddy javap have nothing to do with use of constructor. javap is Java bytecode disassembler, all you see after using this command is execution of working bytecode.
    Are you sure? This action cannot be undone.
    Cancel
  • anandkumarjha

    MemberSep 2, 2010

    hey leo....are you trying to say that default constructor is not being created while compilation of the java program??
    Are you sure? This action cannot be undone.
    Cancel
  • Leo

    MemberSep 2, 2010

    No i want to say javap is Java byte-code disassembler and had to do nothing with above constructor discussion.
    Are you sure? This action cannot be undone.
    Cancel
  • gavinol

    MemberSep 2, 2010

    Java is a very ideal platform for network computing. This is one of the best & all-rounder known technology that is used from servers to cell phones to smart cards... If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler.

    <snipped - ES>
    Are you sure? This action cannot be undone.
    Cancel
  • xxxabhash007

    MemberSep 2, 2010

    Java follows UNICODE.
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberSep 2, 2010

    public class r
    {
    //public r();//you must need this to initialise the class r so this is why "r()" is bydefault.
    public static void main(String args[])
    {
    int x=2;
    System.out.println(x);
    }
    }
    One more thing javap is the command which display all the members of class and constructors used in the public class.
    Are you sure? This action cannot be undone.
    Cancel
  • sushant005

    MemberSep 3, 2010

    Leo
    No i want to say javap is Java byte-code disassembler and had to do nothing with above constructor discussion.
    I know that javap is nothing to do with the constructor creation.It is just a command which shows that a default constructor is created by the complier only if no constructor is defined in the class definition.
    My actual question why a default constructor is created?
    Are you sure? This action cannot be undone.
    Cancel
  • anandkumarjha

    MemberSep 3, 2010

    sushant005
    I know that javap is nothing to do with the constructor creation.It is just a command which shows that a default constructor is created by the complier only if no constructor is defined in the class definition.
    My actual question why a default constructor is created?
    Because for operation of the actual class atleast one constructor is required so if you dont mention any of the constructor then while compilation it will create its default constructor.as far as creation of constructor is concerned we must be knowing that it initialises the variable and without initialise of variable we can't access them,so default constructor is being created
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register