0

I want to develop a library with a new idea for Android applications but I have problem. How can I call the method after another method?

for example...

Toast.makeText(context, "", Toast.LENGTH_SHORT).show();

my codes

MyClass class

public class MyClass {

    public static MyClass A(){
        return A();
    }

    public static void show(){

    }

MainActivity Class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //It's normal
    MyClass.A();
    //if he wrote like this I want tell him error
    MyClass.show();
    //I want like this
    MyClass.A().show();

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Taha Sami
  • 1,565
  • 1
  • 16
  • 43
  • You do just what you did there- chain them if allowed. Or write a function that calls A() then B(). If you mean something more than that, you need to be much more clear about what you're asking. – Gabe Sechan Aug 09 '18 at 14:00
  • Do you means you wants to know what your first method should return in order to call the 2nd one? If that's it, the answer is: Any kind of object that has method. You question is way to broad – jhamon Aug 09 '18 at 14:02
  • I only want to show the method after calling a particular method , but if he wrote Toast.show(); do not show. – Taha Sami Aug 09 '18 at 14:05
  • @jhamon yes I want that – Taha Sami Aug 09 '18 at 14:06
  • https://stackoverflow.com/questions/21180269/how-to-achieve-method-chaining-in-java ? You can only do `thing.a().b().c()` if `thing.a()` returns some object with a `b()` method, and then that `b()` method then returns an object with a `c()` method, etc. `thing.a().b()` is equivalent to `BThing bt = thing.a(); bt.b()` without making `bt` a visible variable in the code. – zapl Aug 09 '18 at 14:27
  • 1
    `public static MyClass A(){ return A(); }` This will cause a StackOverflowError.. – Kevin Cruijssen Aug 09 '18 at 14:34

3 Answers3

0

What you want is irrelevant.
What is important is the way classes and methods work in Java.
Since you declare show() as static then the only way to call it is:

MyClass.show();

If you remove static keyword then you can do this:

   MyClass myClass = new MyClass();
   myClass.show();
  • how does that answer the question? – jhamon Aug 09 '18 at 14:04
  • @jhamon OP asks how can he call show after he creates the toast, did you read? –  Aug 09 '18 at 14:05
  • you should call up show method after makeText() method but you can't use Toast.show(). , This is what I want the method to call only after calling a particular method in my library – Taha Sami Aug 09 '18 at 14:08
  • @TahaSami This is what this code does. It prepares the Toast and shows it when you need it. –  Aug 09 '18 at 14:10
  • @TahaSami see my edited answer –  Aug 09 '18 at 14:17
  • This is an example, but I have my library , for example I have Color class inside class there 2 methods A(); show(); I if I wrote Color.A(); will showing and if I wrote Color.show(); will showing but I want if he wrote Color.show(); now showing but if he wrote Color.A() so show will showing like this , Color.A().show(); – Taha Sami Aug 09 '18 at 14:18
  • 1
    Post your code so we can stop guessing what you want – jhamon Aug 09 '18 at 14:21
  • @TahaSami If you want to get help you must phrase clearly your problem. At this case you are not. Edit your question with beter phrasing and the needed code. –  Aug 09 '18 at 14:24
  • Well I will change my participation Wait 5 minutes – Taha Sami Aug 09 '18 at 14:26
  • Modified you can see now – Taha Sami Aug 09 '18 at 14:32
  • @TahaSami So what did all this have to do with Toast? –  Aug 09 '18 at 14:52
  • The problem was resolved at the top , thank you for your time :) – Taha Sami Aug 09 '18 at 14:54
0

It is a litte hard to understand what you want, but maybe this is what you are looking for:

public class Toast {
    private String text = null;

    public Toast makeText(final String text) {
        this.text = text;
        return this;
    }

    public void show() {
        if (text == null) throw new IllegalStateException("Text must be set before you can call show.");
        System.out.println(text);
    }

    public static void main(final String[] args) {
        final Toast toast = new Toast();
        toast.makeText("test").show();

        final Toast toastError = new Toast();
        toastError.show();
    }
}

By returning the instance of the object in the makeText-method you can call show afterwards.

knutesten
  • 594
  • 3
  • 16
0

No idea why you want it, but maybe something like this:

static class MyClass{
  private static MyClass instance;

  public static MyClass A(){
    if(instance == null){
      instance = new MyClass();
    }
    return instance;
  }

  public void show(){
    // TODO: Do something
  }
}

And you can now call it like this:

MyClass.A().show();

Try it online.

MyClass is now a Singleton, which only has a single instance alive. I would advice renaming MyClass to MySingleton and A() to getInstance(). (Also, if you're using it in multiple threads and want to make it thread-safe, you can add the synchronized keyword to the method. But I guess it's single-threaded considering you use it in your Android Activity class, which uses the default UI-thread.)

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135