And so, while studying the Java language, I just can’t understand what a reference type is and why it needs to be specified when declaring an instance of a class. With the class
String
, for example, everything seems clear:
//Создаётся link типа String
String s = "Ссылка на an object, представляющий собой строку";
But all clarity disappears when a similar approach is used: Let's say there are two classes: A (с методом a1)
and B (с методом b1)
.
class A
{
public static void a1()
{
///
}
}
class B
{
public static void b1()
{
///
}
}
There is also a class with main
a method in which, for example, an object of the class is created AB
.
class GetTypeVar
{
public static void main(String[] args)
{
A obj1 = new A();
B obj2 = new B();
}
}
So, actually, what is the misunderstanding:
- What does the type of the variable being created mean in this case?
- What data type do you mean?
class GetTypeVar
{
public static void main(String[] args)
{
B obj1 = new A();
A obj2 = new B();
}
}
That is, a link leading to an instance of one class is set to the class type of another. For what? What does this give? In general, the creation of this topic was inspired by thoughts in search of truth. I will be glad to hear quality parting words...
GO TO FULL VERSION