M
M
mazt_ser2020-11-17 15:43:32
Java
mazt_ser, 2020-11-17 15:43:32

How to get the value of a class field in Java from a class reference?

There is a creation of the List class, in which there is a link field.
The reference to the sheet class is passed to another variable, how to get a field from the created class from another variable.
Code example:
List lastElement = new List();
Object testLink = lastElement;
Object a = lastElement.link; //works as it should
Object b = testLink.link //does not work, but it is necessary that it works through a variable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
acwartz, 2020-11-17
@mazt_ser

And it won't because

<b>List lastElement</b> = new List();
<b>Object testLink</b> = lastElement;
Object a = lastElement.link; //работает так, как нужно
Object b = testLink.link //не работает, а нужно, чтобы именно через переменную работало

testLink is an Object type that does not have a link property, while lastElement has such a property, and being a descendant of Object it can be cast to it.
The solution might be to cast the Object type back to a List:
Object b = ((List)testLink).link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question