sathishhanumandla
sathishhanumandla

Reputation: 113

Who will initialize the data members? Is it a constructor or any thing?

If it is default constructor, who will initialize the member variables to zero then how come this will be possible

class A        
{        
    public int i;        
    public int j;        
    public A()        
    {        
       i=12;        
    }        
}        
class Program        
{        
     static void Main(string[] args)        
     {        
         A a = new A();        
         Console.WriteLine(a.i + "" + a.j);        
         Console.ReadLine();          
     }        
}

Upvotes: 6

Views: 1408

Answers (7)

nuli bunny
nuli bunny

Reputation: 17

If in your program, there is no prog-defined constructor then the system will create a default constructor during the time of compilation which is known as "system defined default constructor".

system-defined default constructor - will assign the default values to the data members of your class.

You can check this out --> Do not define any constructor in your class and compile your java program.After you compile type the command javap className and press Enter. You will find your class definition in which you will have default constructor and it does not have any statement in body part provided by the compiler.

Upvotes: -1

dyatchenko
dyatchenko

Reputation: 2343

A field initialization happens before a constructor call through the newobj command. It is easy to check after decompilation of the executable with the following C# code:

using System;
namespace ConsoleApplication1
{
    public class A { public int j; }

    class Program
    {
        static void Main(string[] args)
        {
            A g = new A();
            Console.WriteLine(g.j);
        }
    }
}

Part of the decompilted MSIL code (method main):

//000017:         {
    IL_0000:  nop
//000018:             A g = new A();
    IL_0001:  newobj     instance void ConsoleApplication1.A::.ctor()
    IL_0006:  stloc.0
//000019:             Console.WriteLine(g.j);
    IL_0007:  ldloc.0
    IL_0008:  ldfld      int32 ConsoleApplication1.A::j
    IL_000d:  call       void [mscorlib]System.Console::WriteLine(int32)
    IL_0012:  nop
//000020:         }

As we can see the MSIL uses the newobj instruction to create an instance of the A class. According to the following microsoft acticle:

The newobj instruction allocates a new instance of the class associated with ctor and initializes all the fields in the new instance to 0 (of the proper type) or null references as appropriate. It then calls the constructor ctor with the given arguments along with the newly created instance. After the constructor has been called, the now initialized object reference (type O) is pushed on the stack.

If it is wrong, please comment, otherwise assign it as a right answer, please.

Upvotes: 11

Jason Cidras
Jason Cidras

Reputation: 507

In C#, int variables will always default to 0 unless otherwise specified. In your constructor:

public A ()
{
    i=12;
}

When your object is instantiated, your i property will be initialized to 12, while your j property will be initialized to 0.

In your code:

public class Program
{
    A a = new A(); // You instantiated your object, i will be assigned 12
    Console.WriteLine(a.i.ToString() + "" + a.j.ToString()); 
    // Your output will be 12 0 because you didn't initialize j so the default is 0
    Console.ReadLine();
}

I hope this helps!

Upvotes: 2

Soner Gönül
Soner Gönül

Reputation: 98750

Because field initializers run before constructors. You didn't assign j value in your constructor, it will be 0 by default since it is an int.

From 10.4.4 Field initialization

The initial value of a field, whether it be a static field or an instance field, is the default value (Section 5.2) of the field's type.

Who do that? Well, looks like newobj does that when you call new. Take a look dyatchenkos' answer for more details.

Upvotes: 18

Kavindu Dodanduwa
Kavindu Dodanduwa

Reputation: 13059

C# set default int to 0 . More about default values found here

And further more since they are fields this apply

The initial value of a field, whether it be a static field or an instance field, is the default value.

Default constructor will assign i to 12 but j will have 0 as the default value since it haven't been assigned anywhere in the constructor.

Upvotes: 1

haggy
haggy

Reputation: 106

During the instance creation, its variables are made available too. And since int is not nullable, it initializes with default(int). Which is 0

Upvotes: 1

Blaatz0r
Blaatz0r

Reputation: 1205

The answer lies in the fact that c# numeric fields are 0 by default. While in C++ the int field is some random value by default

Upvotes: 1

Related Questions