Class and instances of the class question.

When I write a line of code like this:

private someclass someclass2;
someclass2 = new someclass();

Or a code like this:

private someclass someclass2;

I get the same functionality.
So what exactly is the use of the line: someclass2 = new someclass(); ?

When you first declare a private member variable, that variable will have a value of null.

private Car myCar; // myCar == null here

At this point, you can’t use this variable, because it’s null! For example:

myCar.DriveSomewhere(); // This will throw a NullReferenceException!

Using the “new” keyword runs the constructor of that class, and returns you a new instance of that class.

myCar = new Car(); // After this line, myCar will point to a new Car

Now you can use your variable, since it is a Car:

myCar.DriveSomewhere(); // This will now successfully run the DriveSomewhere method