NewNoobOntheBlock
NewNoobOntheBlock

Reputation: 23

making a variable an instance of a class

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();
}

Upvotes: 2

Views: 281

Answers (4)

Marcel Toth
Marcel Toth

Reputation: 11026

Structurally:

class ClassName
{

  //... class/object level member variables can be here (also called fields)

  void MethodName()
  {
    //... method level variables can be here (also called local variables)
  }

  //... class/object level member variables can be here (also called fields)

}
  • Is the member variable class or object level ?

    • class level variables use the static modifier
    • object level variables DO NOT use the static modifier

So in the definition of the class MyClass2 you've got one object level member variable with name whatisThis and with type MyClass1. (It is object level variable, because there is no static keyword)


Now to the definition of the variable itself. Let's divide this definition into 3 parts:

MyClass1 whatisThis = new MyClass1();
  1. MyClass1 whatisThis
    • This is the variable definition
    • Variable is of type MyClass1, this is the type name, same as int in int i;
    • Variable has name whatisThis, this is the variable name, same as i in int i;
  2. =
    • This is assignment operator
    • Assigns reference to object into the variable whatisThis
  3. new MyClass1()
    • This is instantiation of the object
    • object has type of MyClass1
    • object has no name, it only has its address in heap memory

Also, let's define, what is happening in memory, when the line is executed (this line is only executed, when MyClass2 gets instantiated):

  1. MyClass1 whatisThis
    • empty reference is created in the instance of MyClass2 in the heap
  2. =
    • empty reference is filled with the address of new MyClass1 type object in the heap
  3. new MyClass1()
    • object of type MyClass1 is instantiated in the heap

So the final answer is:

In the class MyClass2, you are defining the object-level variable whatisThis of type MyClass1 and you are initializing it with reference to object instance of type MyClass1 that is instantiated on heap.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062630

That is an instance field that is initialized to a new instance; encapsulation of a MyClass1 would be my description. If you were re-exposing an interface common to both types, then, it might be decoration.

Of course, with no methods, a private field can't do a lot ;p

So to clarify:

  • whatisThis is a field (private and per-instance, in this case) of type MyClass1
  • new MyClass1() is a new object of type MyClass1
  • the reference to the new object is then assigned to whatisThis during construction; field initializers (to use the name of this assignment) typically happen before any custom constructors are invoked

Upvotes: 2

iammilind
iammilind

Reputation: 69988

What is this actually called?

You are declaring a new object of MyClass1 with operator new (which allocates memory for the object)

"whatisThis" is this a class object?

Yes

Upvotes: 0

Xeo
Xeo

Reputation: 131789

That's just what it looks like - a member variable, and additionally it gets directly initialized. It isn't any different from primitive types like int and the likes. You can use it at your will inside of your MyClass2.

Upvotes: 1

Related Questions