Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

android studio - Call method in java without object or class name

Button myButton = (Button) findViewById(R.id. my_button)

As much I know in java all code have to be on a class body, that said if we invoke a method we must have to call it through object or if it's a static method then through its class name like bla.findViewByID(somemoreblass)

But in android studio I see this Button myButton = (Button) findViewById(R.id. my_button)

Can anyone explain how it works?

question from:https://stackoverflow.com/questions/65602654/call-method-in-java-without-object-or-class-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

findViewById() is a method in the Activity class from which you are extending given you are in an Activity. Just like any method which is defined within a class or parent class, this method can be called without using a class name or reference.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...