0

I am working on an exercise that asks me to create a class "Customer" then "declare a class variable named customer of type customer with an initial value of null." Now I was thinking this means instantiate an object of the class Customer, meaning the correct code would be Customer customer = new Customer();

However, I did not know how to set it equal to null so I looked up the solution and it said to write it like this private Customer customer = null;

To me it seems that this is bypassing calling the constructor and I am having trouble understanding exactly what is happening when I write it the way it says to in the solution.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Paul
  • 53
  • 6
  • Yes, it isn't calling the constructor, you create a variable with value `null` (meaning it will throw an exception if you try to use any member of the class through this variable) – UnholySheep Mar 06 '19 at 19:50
  • 1
    See: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/null – UnholySheep Mar 06 '19 at 19:50
  • FYI: Not related to your `null` question, but be aware of potential confusions and misunderstandings if you use terms incorrectly. A "[_class variable_](https://en.wikipedia.org/wiki/Class_variable)" -- in C#/.NET more commonly called a "static field" -- is not the same as an "[_instance variable_](https://en.wikipedia.org/wiki/Instance_variable)" (this is what you seem to have here). You don't need to know about this yet, but just be aware that the terms you are using might already have an established definition that is quite different from what you mean when using them... –  Mar 06 '19 at 19:56
  • @UnholySheep Ok so what I am having trouble understanding is whether its still an instantiation of the class Customer or not? I guess maybe the context for why you would even do that to begin with is lost on me. The program is basically just taking in customers names and emails and adding them to a list. This particular variable was created so it can be returned in a method that says "public Customer GetNewCustomer(){ this.ShowDialog(); return customer;} – Paul Mar 06 '19 at 19:57
  • @elgonzo thank you for the clarification, I think that is very relevant because part of the problem I am having is understanding why it is not a class variable and what its function is at all – Paul Mar 06 '19 at 19:59
  • With reference types, a variable is simply a reference to an object created separately. A variable (of reference type) has a default value of _null_, which means that it does not refer to an object. So you can say `Cust c = null;` to create the variable, but not assign it to an object. You can create an object with a `new` expression `c = new Cust();` and assign that object to multiple variables `var c2 = c;`, at which point both `c` and `c2` will refer to the same object. If you mutate the object `c` refers to, then you can see the change via `c2`. Objects exist, variables refer to them. – Flydog57 Mar 06 '19 at 19:59
  • No, there is no instantiation and you have no object: *"The `null` keyword is a literal that represents a null reference, one that does not refer to any object"*. As for the context, I'd assume that the object is created at some other point in the code (constructor, some method, ...) – UnholySheep Mar 06 '19 at 19:59
  • If you think it is relevant for your learning, or if you are just curious, see the Wikipedia links in my previous comment about class variables vs. instance variables... :-) –  Mar 06 '19 at 20:02
  • As to your question _whether its still an instantiation of the class Customer or not_ and why you'd do this. A (reference type) variable is typed, and it has value. The value can either be _null_ or be a reference to a fully instantiated object of the correct type (or a sub-class of the correct type). Why would you do that, consider: `Cust c=null; if (left) {c=new Cust (ABC); } else if (right) {c = new Cust (XYZ);} return c;` Your function may return null if neither left nor right are true, or, it may return instances of Cust created differently – Flydog57 Mar 06 '19 at 20:10

0 Answers0