- Home /
Creating a data type instance difference question
Hey, is there a difference between creating these two kinds of data types.
Class someClass{
}
someClass object1;
someClass object1 = new someClass();
Is there a difference ?
-Thanks, Karl
They are both variables to hold someClass type of objects. There is no difference between the two object1 variables themselves. In the first case no object is assigned to the value of object1 will be null. In the second case you instantiate a new someClass object and store the reference to it in the object1 variable.
Thanks for the answer, what you mean by "null", i would still be able to access variables from if using the first case. For example:
Class someClass{
int a = 5;
}
someClass obj;
obj.a = 10;
I know there will be created a new copy of that class, but that object wont be null, because i can still access a from it ?
If you tried what you just typed, you'd get a null reference error.
obj
doesn't exist as something concrete yet. That requires use of the "new" keyword or the use of statics (which are a different beast all together).
null is a special value that effectively means that 'no object is assigned'. Trying to access fields of a null variable will raise an error as it doesn't point to any real object in memory.
Thanks, i understand now, this is the reason why you shouldn't switch to C++ time to time heh.
@Dracorat Convert your comment to answer
Answer by Dracorat · Nov 01, 2013 at 06:20 PM
Neither are data.
There is a difference, but consider this:
A Foreman gets a blue print for a new office building. He looks at the blue print.
Is the blue print an office building? No - it describes one.
He builds an office building from it. Is that an office building? Yes. Is is a blue print? No. Was it generated from a blue print? Yes.
He builds another office building. Is that an office building? Yes. Is it the same office building? No. If you smash out a window in it, will a window in the other be smashed out? No.
The class is a "blue print" from which you create new objects. The objects are the constructed instance of the class. It is something that exists in game terms. The class contains only instructions for how it should exist.
That's the difference.
Thanks, but, lmao, i didn't understand any of it. Good that you didn't take hookers as an example haha.
Your answer
Follow this Question
Related Questions
How to convert System.Type into an instance or access the class variables 3 Answers
Get Superclass of Subclass in Unityscript 2 Answers
Is every Script I drag on a Gameobject an instance? 1 Answer
How to call a new instance of a class in C# 1 Answer
help with creating a static Instance in javascript 2 Answers