user1152440
user1152440

Reputation: 905

Java: Calling function from different classes

I'm very new to Java which is required for a Blackberry App development project (which is what I'm doing now). My issue is that I am trying to use this class I found online (attached below) to implement a notification feature in my application. As I said I'm extremely new to Java so I don't know how to call the setVisible1 from another class, say UserInterface. I have tried MyAppIndicator._indicator.setVisible1(true,1); but this produces an error of:

"The method setVisible1(boolean, int) is undefined for the type ApplicationIndicator".

Any help would be appreciated. Thanks!

public class MyAppIndicator
{
    public ApplicationIndicator _indicator; 
    public static MyAppIndicator _instance; 
    private MyAppIndicator () {}

    public static MyAppIndicator getInstance() {
        if (_instance == null) {
            _instance = new MyAppIndicator ();
        }

        return(_instance);
    }

    public void setupIndicator() {
        //Setup notification 
        if (_indicator == null) {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
            _indicator = reg.getApplicationIndicator();

            if(_indicator == null) {
                ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("status_icon_24x24.png"));
                _indicator = reg.register(icon, false, true);  
                _indicator.setValue(0);
                _indicator.setVisible(false);
            }
        }
    }

    public void setVisible1 (boolean visible, int count) {
        if (_indicator != null) {
            if (visible) {
                _indicator.setVisible(true);
                _indicator.setValue(count);
            } else {
                _indicator.setVisible(false);
            }
        }
    }
}

Upvotes: 0

Views: 6023

Answers (4)

atk
atk

Reputation: 9314

boolean visible = ...; // you need to set something here
int count = ...; // you need to set something, here
MyAppIndicator mai = MyAppIndicator.getInstance();
mai.setVisible1(visible, count);
  • setVisible1() is is a regular, public method, so any other class can invoke the method on an instance of the MyAppIndicator class.
  • you need to pass the same number & type of arguments to setVisible1() as are in its definition. The definition reads setVisible1(boolean visible, int count), so you need to pass a boolean and an integer. Java convention is that passing "true" will likely make the indicator visible, and "false" will hide it. The int is unusual, but reading the code it's ignored, so pass whatever you want.
  • to get an instance of a class, you normally write new ClassName. However, MyAppIndicator declares the constructor as private, so other classes cannot invoke it. Instead, myAppIndicator provides a getInstance() method that will handle object construction for you.

You should also read the Java Tutorial - they've got a far better description of all this.

Upvotes: 0

hovanessyan
hovanessyan

Reputation: 31433

In order to call methods from Classes in Java, you need instance of the class (or if the class is 'static' you can directly use the methods, as 'static' basically means only 1 instance).

In your case MyAppIndicator implements Singleton:

public static MyAppIndicator _instance;
private MyAppIndicator () {};

public static MyAppIndicator getInstance() {
    if (_instance == null) {
        _instance = new MyAppIndicator ();
    }

    return(_instance);
}

This means that when you call getInstance() it will return instance of MyAppIndicator if such an instance already exists, or it will create a new one, if there's no instance.

After acquiring an instance of a class, you simply call it's methods, if they have the appropriate access modifiers. In your case setVisible1 has access modifier 'public' so you will be able to call it outside of MyAppIndicator.

So in code:

a) acquiring instance:

MyAppIndicator instanceOfMyAppIndicator = MyAppIndicator.getInstance();

b) calling the method:

instanceOfMyAppIndicator.setVisible1(true,1);

Upvotes: 1

krackmoe
krackmoe

Reputation: 1763

You can do the following:

MyAppIndicator myAppIndicator = MyAppIndicator.getInstance();
myAppIndicator.setVisible1(true,1);

Upvotes: 0

Rabi
Rabi

Reputation: 111

Looking at the above code, you should call

MyAppIndicator.getInstance().setVisible1(true,1);

than

MyAppIndicator._indicator.setVisible1(true,1);

Upvotes: 0

Related Questions