-1

I am trying to learn how to create an interface. I have read different articles and seem to be missing some basic concept.

I want to create a class that when it is done doing something, it will notify any listening classes that it has finished. For example, I want to instantiate a version of MySingletonClass in my MainActivity. Have MySingletonClass do something from MainActivity and then when finished, call someMethod that has been Overridden in MainActivity to

But when I create my interface I get the error:

Class is public, should be declared in a file named filename.java

Here is the error on screen:

enter image description here

Here is my code:

package com.mycompany.myapplication;

public interface MySingletonInterface {
    void someMethod();

}

public class MySingletonClass {
    private static MySingletonClass ourInstance = new MySingletonClass();

    private MySingletonInterface msi;

    public static MySingletonClass getInstance() {
        return ourInstance;
    }

    private MySingletonClass() {
    }

    public void doSomething(){
        msi.someMethod();
    }

}

Is this the way I should be doing it? Is there some better best-practice? Again I am learning java/android.

I come from an iOS background and in iOS we have what is called a delegate pattern, it's a way for a class to notify another class that it has finished doing something. Is there something like that in Java?

user-44651
  • 3,924
  • 6
  • 41
  • 87
  • 1
    Error message is quite clear. `MySingletonInterface` should be in `MySingletonInterface.java` and `MySingletonClass` in `MySingletonClass.java`. – Eran Nov 01 '16 at 18:48
  • 1
    Either remove the word `public`, or move the interface and class to separate suitably named files. – khelwood Nov 01 '16 at 18:48
  • 1
    `MySingletonInterface` should be its own file. If it is only going to be used by `MySingletonClass` then it should really be marked private. – Chris Stillwell Nov 01 '16 at 18:48
  • It's best if you put each class or interface in its own file. The name of any public class or interface has to match the name of the file. – Dawood ibn Kareem Nov 01 '16 at 18:48
  • Then what is the purpose of having an interface? Couldn't I just `implements` MySingletonClass? – user-44651 Nov 01 '16 at 18:49
  • `public` means that every class should be able to access it, which means it needs to be in its own file - how else would other classes find it? If you intend to use the interface for this package only, don't use any access modifier keyword - simply remove `public`. – RaminS Nov 01 '16 at 18:51
  • While it's a good exercise for learning Java, please see [Why is Singleton considered an anti-pattern?](http://stackoverflow.com/q/12755539/5743988). – 4castle Nov 01 '16 at 18:52
  • I come from an iOS background and in iOS we have what is called a delegate pattern, it's a way for a class to notify another class that it has finished doing something. Is there something like that in Java? – user-44651 Nov 01 '16 at 18:53
  • Yes, the delegate pattern can be used in Java. [Google is your friend](https://www.google.com/search?q=java+delegate+pattern&oq=java+delegate+pattern&aqs=chrome..69i57j0l4.5928j0j4&sourceid=chrome&ie=UTF-8) – 4castle Nov 01 '16 at 18:56

1 Answers1

1

Interface and class have to be in separate files. I don't see what the interface is doing for you as written.

You're learning Java. Forget about Singletons. They aren't a good idea.

MySingletonInterface.java:

package com.mycompany.myapplication;

public interface MySingletonInterface {
    void someMethod();

}

MySingletonClass.java:

package com.mycompany.myapplication;

public class MySingletonClass implements MySingletonInterface {

    private static final MySingletonClass ourInstance = new MySingletonClass();

    private MySingletonClass() {}

    public static MySingletonClass getInstance() {
        return ourInstance;
    }

    public void someMethod(){
        // Implement here
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Singletons aren't a good idea? Why? If I need one instance of an object, but I need to reference it in different activities, how do you do that? – user-44651 Nov 01 '16 at 18:50
  • Google has gone to great lengths to expunge Singletons from all their code. You should do the same. http://googlecode.blogspot.com/2007/07/google-singleton-detector-released.html – duffymo Nov 01 '16 at 18:52
  • Read the link I gave you. Global variables and bottlenecks are never good. You may think you only need one, but the requirement is usually more imagination than fact. Dependency injection solves it easily: Inject a reference into the objects that need it. – duffymo Nov 01 '16 at 19:14