understanding the logic of class object name repeats twice question

Thread Starter

yef smith

Joined Aug 2, 2020
374
Hello,when i create and object of type class i need to write as shown bellow.
I understand the left side of the line
Code:
TestClass1 f = new TestClass1();
but why we duplicate the same name "new TestClass1();"
What will happen if i will do?
Code:
TestClass1 f = new TestClass2();
Thanks.
 

BobTPH

Joined Jun 5, 2013
5,458
TestClass1 on the right side names a constructor for the class. It is always the same as the name of the class.
 

Thread Starter

yef smith

Joined Aug 2, 2020
374
Hello BobTPH ,So the right right is for choosing which type of constructor to use?
in case we have several constructors?
Thanks.
 

WBahn

Joined Mar 31, 2012
26,841
If you want to create two object, then you need two unique names for the objects.

Code:
TestClass1 f1 = new TestClass1();
TestClass1 f2 = new TestClass1();
 

Papabravo

Joined Feb 24, 2006
18,968
The two occurrences of the identifier "TestClass1" are syntactically almost the same but semantically different. On the left-hand side of the object name, it declares the type of the object f1. With the empty parentheses "()" appended to the name of the class, it calls out the default constructor method for the object. That is the semantic difference. The default constructor is not really a method and has no return type. The name for the default constructor must match the name of the class. Apparently, other constructors with parameters may have other names.
 

WBahn

Joined Mar 31, 2012
26,841
The two occurrences of the identifier "TestClass1" are syntactically almost the same but semantically different. On the left-hand side of the object name, it declares the type of the object f1. With the empty parentheses "()" appended to the name of the class, it calls out the default constructor method for the object. That is the semantic difference. The default constructor is not really a method and has no return type. The name for the default constructor must match the name of the class. Apparently, other constructors with parameters may have other names.
Some of what you say is language dependent and I didn't see where the TS indicated a specific language.

In general, the constructor must return a value that is of that object type, but because this is the case the syntax of most OOP languages leave this as implied so that the programmer can't mess it up. Some languages require that the return type be specified explicitly. Also, while some languages require the constructor name to match the name of the class, others do not. But most, if not all, of the OOP programs in current widespread use impose these constraints as part of their obsession with abstraction.
 

Papabravo

Joined Feb 24, 2006
18,968
Some of what you say is language dependent and I didn't see where the TS indicated a specific language.

In general, the constructor must return a value that is of that object type, but because this is the case the syntax of most OOP languages leave this as implied so that the programmer can't mess it up. Some languages require that the return type be specified explicitly. Also, while some languages require the constructor name to match the name of the class, others do not. But most, if not all, of the OOP programs in current widespread use impose these constraints as part of their obsession with abstraction.
It is true that I was relying on my knowledge of C++, where the default constructor name must match and does not return a value. I've never worked with Java.
 

MrAl

Joined Jun 17, 2014
9,333
Hello,when i create and object of type class i need to write as shown bellow.
I understand the left side of the line
Code:
TestClass1 f = new TestClass1();
but why we duplicate the same name "new TestClass1();"
What will happen if i will do?
Code:
TestClass1 f = new TestClass2();
Thanks.
Hello there,

It is interesting to look at the entire history of math and programming as we know it. Starting with counting and then adding and subtracting, then eventually to variables like x and y and z and a and b and c and the like.
When we declare a variable we would do something like:
int x;

and that probably predated function where when we declare a function it may go like this:
ulong Calculate(int x);

In each case we had a 'type', either int or ulong and a name, either x or Calculate, and historically a function had to predate a class.

A class is kind of just a mix of these two, where both functions and data are grouped together and work together in some way. When we declare a class though it's just like any other declaration we need a type and a name. We have to have a name and we have to know what class it is going to be. The difference in the given statement may just be the different orientation of the type and the name.
For vars and functions we had:
var type MyName
return type MyFunctionName

and using the 'new' command we have:
MyClassName new ClassType (or ClassName)

where MyClassName is a new instance of the established class called ClassName.

So in the case of vars we had a type of var 'int' and in the case of functions we had a return var type 'ulong' and the function name we wanted to use for the function, and in the case of classes we had a type of class that was already established and a name we wanted to use for a new instance of that class. So the class name is the type of class and the name MyClassName is the variable to use for that new instance of that class which is similar to the way we did it with vars and functions.

With this in mind, suppose we want to declare a safe type array that has already been defined and has many function for accessing the data in that array. The class may already be called the "SafeArray" class. This class would work the same as a regular C type array except we use functions to access the data rather than direct references to memory.
One function would be such as "Get(x,y)" and another "Put(x,y)". The Get function retrieves data at the location x,y and the Put function places data at that location x,y. This would be like any other 2d array except for the way we access the data, and most likely another function to declare the size unless it was declared in the class definition already. Now say we have data coming in where we need three arrays. We could write something like:
FirstArray = new SafeArray
SeoncdArray = new SafeArray
ThirdArray = new SafeArray

and now we have three arrays to work with.

If we were to do this:
SafeArray = new SafeArray

then we have a new SafeArray named SafeArray and that would have functions Get and Put just like before.
The only thing special i think is the context would imply the way the compiler interprets the code that follows.
If you declare another array you cant call it SafeArray too, but you can declare more arrays that are of class SafeArray.
So the way a class definition is handled is different than the names declared of that class. It's almost like it is in a different namespace. The context that follows will indicate how that name is to be used.
For example something like this:
SafeArray:Get(1,2) will get an element from the instance SafeArray at location 1,2
and
MyArray = new SafeArray
will still create a new instance of SafeArray to be used later.
There could be a problem if the compiler can not accept the double name syntax or may trigger a warning, but i think it is allowed in the C++ specifications. Is it a good practice? I dont think i would do this because then the code becomes slightly harder to read.

It's been a long time since i looked at any compiler code, but i would guess that a simple way to handle the double name conundrum would be to enumerate the variables such that the names would include a hidden part in addition to the visible part.
For example, for:
SafeArray = new SafeArray
the compiler may change the name of SafeArray on the left to:
SafeArray_0
and so it sees that statement more like:
SafeArray_0 = new SafeArray

This would not be unusual, and sometimes the names are also shortened internally such as:
instance SafeArray becomes AAAA
and the next SafeArray instance become AAAB.
This would even be the case with:
SafeArray1 = new SafeArray
SafeArray2 = new SafeArray
where SafeArray1 becomes AAAA and SafeArray2 becomes AAAB.
This happens with interpreters so the code can run faster.
When you examine the code source file of course the names are not changed it's only internal.
 
Last edited:
Top