I’m in a-level computing and I was wondering if someone could please tell me the definition of this. If I have a class with methods etc, then I make another class where I kind of generate a variable of that first class. What is this actually called?
It’s the variable definition I’m after shown below “whatisThis” is this a class object?
MyClass1
{
...
}
MyClass2
{
MyClass1 whatisThis = new MyClass1();
}
Structurally:
Is the member variable class or object level ?
staticmodifierstaticmodifierSo in the definition of the class MyClass2 you’ve got one object level member variable with name
whatisThisand with typeMyClass1. (It is object level variable, because there is nostatickeyword)Now to the definition of the variable itself. Let’s divide this definition into 3 parts:
MyClass1 whatisThisMyClass1, this is the type name, same asintinint i;whatisThis, this is the variable name, same asiinint i;=new MyClass1()MyClass1Also, let’s define, what is happening in memory, when the line is executed (this line is only executed, when
MyClass2gets instantiated):MyClass1 whatisThis=MyClass1type object in the heapnew MyClass1()MyClass1is instantiated in the heapSo the final answer is:
In the class
MyClass2, you are defining the object-level variablewhatisThisof typeMyClass1and you are initializing it with reference to object instance of typeMyClass1that is instantiated on heap.