This discussion is archived
3 Replies Latest reply: Nov 9, 2013 10:26 AM by baftos RSS

object creation and constructors

abroshan-Oracle Explorer
Currently Being Moderated

HI all ..

 

I have below confusion plz help me ..

 

1)When we create object , I know non-static members are loaded into the memory .. so are these only non-static attributes or methods also?

2)This object creation , actually calls the compiler constructor (in case we have not created in the program) , so this constructor will help in loading the nonstatic attribute ..

 

thanks

  • 1. Re: object creation and constructors
    gimbal2 Guru
    Currently Being Moderated

    1) No. In the case of the Java language, methods do not need to take up any memory space per object. They're only part of the class definition that Java will load once.

     

    2), that's not a question, but I'll throw some observations at you. A constructor in itself does nothing, you have to put code in there yourself to initialize class members. It is the intended purpose of a constructor to get the object into a state where you can safely use it, notably by making sure that the appropriate class members are filled. But you have to actually make that happen.

     

    Note that ALL classes have a constructor, even when you don't write one yourself.

  • 2. Re: object creation and constructors
    abroshan-Oracle Explorer
    Currently Being Moderated

    Hi thank you ...

     

    1)           class A

    2)            int i;

    3)          A()

    4)          {

    5)         i=10;

    6)          }

    6)         public static void main (String[] args)

    7)         {

    8)          A x=new A();

    9)         System.out.println(x.i);

    10)       }

    11)        }

     

    output 10

    so when the control will come to line 8  , A() will be executed ..and i will get a value of 10 ...and will be printed in line 9.

     

    Now check this one ..

     

    1)           class A

    2)            int i=100;

    3)          //A()

    4)          //{

    5)         //i=10;

    6)          //}

    6)         public static void main (String[] args)

    7)         {

    8)          A x=new A();

    9)         System.out.println(x.i);

    10)       }

    11)        }

     

    output 100

     

    now in this case compiler will be keeping a default no arg constructor .. so when control will come to 8 , that constructor will be executed ..so when does this non static attribute i gets loaded in this case ..

    also what will be that default constructor ..will it be

    A()

    {

    }

     

     

    contructors are used to initialize non static attributes ..

     

    This is a object creation statement

    A x=new A();

     

    So what happens when this is executed ..

    1)All non static members or only non static attributes are loaded?

    2)Then contructors are executed.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points