Reputation: 1
using System;
class Program
{
static void Main()
{
haha haha1;
}
}
class haha
{
int a;
int b;
public haha(int a, int b)
{
this.a = a;
this.b = b;
}
}
I know if I want make instance, I should write code such as:
haha haha1 = new haha(1,2);
but when I write just
haha haha1;
there's no compile error.
What does haha haha1;
mean? isn't it wrong code?
Upvotes: 0
Views: 49
Reputation: 11598
That depends on the programming language, but most programming languages choose one of two options:
1) Implicit initialization (C++)
haha haha1;
is the same as
haha haha1 = new haha1();
2) Null initialization (C#)
haha haha1;
is the same as
haha haha1 = null;
Whatever happens, the result of the behavior is called the "default value" or "default initializer". Refer to your language documentation for details.
Some notes: C and C++ allows variables to be uninitialized, so they have no determinate value until you assign to them. C++ non-pointers cannot be NULL, so they are always initialized with something (but everything else, including pointers, follows the C rule).
Upvotes: 2
Reputation: 103145
It is not wrong code. It is a variable declaration.
haha haha1;
simply declares a variable named haha1 which has the type haha. However this variable was not assigned a value.
When you instantiate the object with the new keyword:
haha1 = new haha();
the variable then refers to the object created
Upvotes: 4