0

I was reading a beginners tutorial on android development which had this line in it's code:

TextView fortune = (TextView) findViewById(R.id.fortune_text);  

I'm fairly new to Java, but I understand the basics of creating variables. So making a new int variable:

int someInt = 4;

Looks a bit different from what is going on above. So I guess we are creating an instance of the TextView object, and then we are calling the findViewById method from the TextView class? But why the (TextView) bit? What are we telling java to do here?

5 Answers 5

3

The casting in your example has to do with inheritance. The method findViewById returns an instance/object of the class View - i.e. it returns a View/widget. TextView, which is the kind of object you want to get access to, is a subclass of View. The casting bit - (TextView) - is your way to inform the compiler of your intentions, that is that you want access to the extended set of methods provided by a TextView and not just any View.

It would probably be worth checking http://download.oracle.com/javase/tutorial/java/IandI/subclasses.html for more details.

Sign up to request clarification or add additional context in comments.

7 Comments

Can you tell me why we need to cast at all? Shouldn't java be knowing this when we've written "TextView Fortune =" in the beginning?
It's because you're downcasting - from super to sub class. This requires that you write the sub type in brackets.
You need to explicitly state this as findViewById might return you another kind of View, in which case your program could break unpredictably during runtime, and so Java doesn't let you do that. When you cast explicitly the runtime throws an Exception if another kind of View is returned; therefore making it easier for you to test and debug.
All objects inherit from Object, so you could declare it like that, but - for example, if you then used it in a method call, it would look for an object signature, instead of the actual type etc.
An Object is not a TextView, a TextView is an Object (and a View).
|
1

findViewbyId() returns a View class. TextView is a subclass of View, and it's casting it to TextView with the (TextView) part.

1 Comment

What does it mean when you say it's casting it to TextView?
1

So I guess we are creating an instance of the TextView object, and then we are calling the findViewById method from the TextView class?

The TextView fortune part does not create anything, just declares a variable of type TextView.

The findViewById() method returns a View instance, which is the parent class of all widgets. In order to use it as a TextView, you have to cast it first.

2 Comments

Aaah, this makes a whole lot of more of sense now. But why declare it as a variable type of TextView? What does that do to the variable? Why not just tell java : Object fortune = blablabla... ?
If you write Object fortune = blablabla then you can't use any of the methods of the TextView class, only those of the Object class (not so useful, in this case).
1

Basically, the left part of the statement is what you're assigning - the right part (after =) is the source of the value / object:

TextView fortune = (TextView) findViewById(R.id.fortune_text); 

The above line declares an object variable of the TextView type, and assigns it to the return of the method findViewById(String). The (TextView) part ensures that the method return is cast to a TextView object. To copy the object, you could write:

TextView someFortune = fortune;

Which is much closer to the syntax of your next line (as you're assigning the same / compatible types).

The next line:

int someInt = 4;

Declares an int, and just assigns its value (without a method call). Because "4" is an int, there's no need to explicitly cast its type (it's primitive anyway).

Update:

Explanation of object type casting:

http://www.javabeginner.com/learn-java/java-object-typecasting

Comments

0

Look at the answer by geek_ji posted here "Why do you at all need a typecast" for a very basic understanding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.