-3

I want to pass the reference of private final class into the method of another class please help me. Thanks

This is the class which I declared in a separate class file and I want to use it

private final class Mute extends BooleanControl {
        private Mute() {
            super(javax.sound.sampled.BooleanControl.Type.MUTE, false, "True", "False");
        }

In this method

public class BaseCameraActivity extends AppCompatActivity {

 protected void onCreateActivity() {

findViewById(R.id.btn_mute).setOnClickListener(v -> {
        //here i want to use the private class mute
    });
Abdullah Nagori
  • 441
  • 1
  • 5
  • 10
  • 2
    How did you manage to declare a top-level class private? Did you check your code compiles? [hint: it won't] – kryger Apr 10 '19 at 13:55
  • 2
    Why? `private` means `private`. It is not intended to be used elsewhere. Consider rethinking your design. Or is this external code which you absolutely can not change? In that case, you could try to hack your way through using reflection. Never consider reflection if you can change the source. – Zabuzard Apr 10 '19 at 13:55
  • @Zabuza Hi, this is the open source project in Github I clone it into my IDE and the private class mute is declared in the protected file so I cannot change it and I want to use this private class into the method of onCreateActivity() please help. Thanks – Abdullah Nagori Apr 10 '19 at 14:09
  • It is a private class, which means it is to be used in the file/class where it is declared... Only there, you can do `Mute mute = new Mute();`. That means, you cannot use it in `BaseCameraActivity` directly. You can add it there, but then you have *some* code redundance... – deHaar Apr 10 '19 at 14:11
  • @JakeJones As I said, if it is external code and you can not change it, reflection is the only way to force-hack your way through. Note that reflection is never a good choice. Having said that, take a look at threads like [Accessing non-visible classes with reflection](https://stackoverflow.com/questions/15015675/accessing-non-visible-classes-with-reflection). – Zabuzard Apr 10 '19 at 14:22
  • @deHaar Thank you so much for your response :) `private mute class` package name is different and where I want to use it package name is different I also import package name like `import com.sun.media.sound.*;` and still, I can't access the class can you please show it by doing code. Thanks – Abdullah Nagori Apr 10 '19 at 14:56
  • Hi, @Zabuza Thanks for your cooperation please read my above comment I can't access it my brain is totally stuck I don't understand how to do it can you please also show it by doing code. Thanks – Abdullah Nagori Apr 10 '19 at 15:00
  • If you can not post a [mcve] (emphasis on complete) to explain your issue in a way that somebody can fully answer it without a back and forth discussion, then this site is probably the wrong site for your question. Because of that, it is likely that it will get closed and down-voted, please read [ask]. – Zabuzard Apr 10 '19 at 16:44

1 Answers1

0

As others have said, the private class can only be used within its definition file. There must be some occurrence of new for that class if it used at all. I will suppose there is indeed a public class in that file (which I call Container)

So search for code that says : new Mute() in the file hosting the class Mute. Ideally you will find a function in the file that does something like :

public BooleanControl createMute() { return new Mute() ; }

BooleanControl (or maybe some interface that BooleanControl itself implements) is an (abstract) reference to your (concrete) "Mute" object that you can use and pass around, and which is not private itself.

i.e. in your client code you get :

Container c = new Container();
BooleanControl  bc = c.createMute();
... use bc...
Yann TM
  • 1,942
  • 13
  • 22
  • thanks for your response you are right `BooleanControl` is a `public abstract class` but still I can't use it please help. thanks – Abdullah Nagori Apr 10 '19 at 15:30