0

I've created this class:

class Remote
{
    private static string name;
    private Button[] button;

    public void delRemote() 
    {
        Remote.name = null;
    }

    public boolean checkAvailable()
    {
        return ((Remote.name) == null);
    }
}

and I want to change checkAvailable so it can check a whole array after I initialize it like so:

Remote remote1[];

Is this possible without having to call checkAvailable n number of times?

5
  • what is the question here ?\ Commented Feb 19, 2014 at 3:50
  • 2
    I don't understand your question. Elaborate more. Commented Feb 19, 2014 at 3:51
  • No..private static string name; --> name is at class level not object level... So, all instances of Remote will use the same copy of name.. Commented Feb 19, 2014 at 3:52
  • Can I change checkAvailable to check a whole array, like remote1? Or is it not possible since I initialize remote1 as an array outside of the class? Commented Feb 19, 2014 at 3:53
  • If your goal is to have check available be false if remote[k].name=null then you can break the loop at return false whenever a case is found. Worst case, you'll have a loop that runs in O(n), which, unless you have some seriously large numbers, is still efficient. Commented Feb 19, 2014 at 3:56

2 Answers 2

1

@user2665581 - See... name is a static field.. which means it exists at class level and all other "instances" of the class share the same field (name).

Now, you don't have a custom constructor. So, by default name will be initialized to null.

If you do,

Remote remote1[] = new Remote[4];

Since name is private you cannot access it from outside the class... And you don't have any method to change the value of name inside the class.. So, basically, with the code which you have given, you cannot change the value of name.. name will always be null... And check availabile will always return true

Sign up to request clarification or add additional context in comments.

Comments

0
              JButton b[]=new JButton[10];
             for(int i=0;i<10;i++)
             {
                 b[i]=new JButton(""+i);
             }
            int size=0;
            for(int j=0;j<10;j++)
             {
                  b[j].setBounds(size,10,10,10);
                  size +=10;
                  add(b[j]);
              }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.