0

I am totally new in android.So may be my question will be like very funny for someone but still i should know the answer of my question-

TextView tv;

tv = (TextView) findViewById(R.id.anyname);

This is the code that i write in java for idtentify the resource id from xml file.As i know (TextView) is a object of View class and findViewById() is a method.Here my confusion is arising. When we invoke any method with the object then we use dot operator(.)-

obj1`.methodName()`

But for the first case there is no dot operator between object and method.So my question is why?

JavaExperts i really need help and suggestion regrading my confusion.

user3732316
  • 63
  • 1
  • 7

1 Answers1

0
But for the first case there is no dot operator between object and method.So my question is why?

That is what you called down-casting a View to TextView.

findViewById will return a View which textView inherits from, what it is doing is that it is down-casting the View to a textView that inherits from it.

Also (TextView) is not an object it is a class.

Community
  • 1
  • 1
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • @Rod_Algonquin-I find this article from android official website.Here it is written (TextView) is a object. An individual UI component, generally referred to as a "widget". Different kinds of View objects include TextView, Button, and CheckBox. – user3732316 Jun 20 '14 at 03:26
  • @user3732316 (TextView) can call its static method but not its public method, and if you create an object from it example. TextView tx ->> now tx is an object of class TextView that can call its public methods. – Rod_Algonquin Jun 20 '14 at 03:35
  • refer to the official java tutorial about object http://docs.oracle.com/javase/tutorial/java/javaOO/ – Rod_Algonquin Jun 20 '14 at 03:35
  • (TextView) findViewById(R.id.anyname); here you are casting the findViewById to TextView that's why you have written `(TextView)`. – Aniruddha Jun 20 '14 at 04:56