dman
dman

Reputation: 11064

Android Java Object/Class Syntax Use In This Example

I am new to Android, but I know Javascript so I have some familiarity in Object Oriented programming.

In this line:

Resources myResources = getResources();
AnimationDrawable androidAnimation;
androidAnimation = 
  (AnimationDrawable)myResources.getDrawable(R.drawable.frame_by_frame); 
  1. is androidAnimation a new object initialized by the AnimationDrawable class?
  2. In (AnimationDrawable)myResources what is this syntax where the class is in parentheses proceeding a object?

Upvotes: 0

Views: 309

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213243

  1. No. androidAnimation is not an object. It is a reference which points to an instance of AnimationDrawable class.

    In the following object creation statement and assigment: -

    MyClass obj = new MyClass();
    
    • Value on RHS - new MyClass() creates an object.
    • Value on LHS - MyClass obj creates a reference to that object.

  2. This is called typecasting. This is done because, the reference type returned by that method invocation might not be compatible with the reference type on LHS. So, a typecast is needed to make it compatible.

For more details about these concepts, refer to JLS: -

Upvotes: 2

Related Questions