fact about java

sushant005

sushant005

@sushant005-tyt4WK Oct 22, 2024
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 ?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • sushant005

    sushant005

    @sushant005-tyt4WK Sep 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.
  • anandkumarjha

    anandkumarjha

    @anandkumarjha-jzdDMA Sep 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
  • sushant005

    sushant005

    @sushant005-tyt4WK Sep 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.
  • Leo

    Leo

    @leo-ZJQlmh Sep 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.
  • anandkumarjha

    anandkumarjha

    @anandkumarjha-jzdDMA Sep 2, 2010

    hey leo....are you trying to say that default constructor is not being created while compilation of the java program??
  • Leo

    Leo

    @leo-ZJQlmh Sep 2, 2010

    No i want to say javap is Java byte-code disassembler and had to do nothing with above constructor discussion.
  • gavinol

    gavinol

    @gavinol-L2K7oX Sep 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>
  • xxxabhash007

    xxxabhash007

    @xxxabhash007-zWy2nM Sep 2, 2010

    Java follows UNICODE.
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Sep 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.
  • sushant005

    sushant005

    @sushant005-tyt4WK Sep 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?
  • anandkumarjha

    anandkumarjha

    @anandkumarjha-jzdDMA Sep 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