Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use static abstract class as a callback in Java?

I'm trying to figure out how I can use a static abstract class as a callback. I'm looking at Android's documentation here and here. The first link is a CameraManager which has a method called openCamera. This method requires an instance of the static abstract callback class StateCallback.

My main question is, how does a static class as a callback provide me anything useful? For example, what I want to do is store the CameraDevice locally from the onOpened callback, but from what I know about static members in C# (I'm a C# dev), this would not be possible because static members cannot access non-static members.

I must not understand something about Java... Or is this an intended consequence of the API design?

Thanks in advance.

like image 359
user3667122 Avatar asked Aug 31 '26 17:08

user3667122


1 Answers

You need to extend it and create a new class that implements those methods, and pass that into the call. Because Java is all references, any parameter can be passed an object that derives from that class as well.

Also a static class in Java just means that it doesn't have a link to its parent object hidden inside of it. It makes it easier for the GC to collect instances. static in C#!=static in Java != static in C++ != static in C. Heck staic in most of those languages mean different things depending on where its used.

like image 61
Gabe Sechan Avatar answered Sep 03 '26 07:09

Gabe Sechan