Hi guys,
I wrote the following code:

public static boolean match(int[] a , int[] pattern)
                {
                    //need to use the overloaded method, in order to work with the static method. 
                    return match(a,pattern,0,0);    
                }


                /** This is an overloaded method that match between a pattern to a given string (as an array)*/
                private boolean match(int[] a, int[] pattern, i, j )
                {


                    if(j==pattern.length)
                        return true;
                    if( (i==a.length) && (j!=pattern.length) )
                        return false;

                    switch(pattern[j]){

                        case 1: if( (a[i]/10)==0 )
                                    return match(a, pattern, i+1, j+1);
                                return match(a, pattern, i+1, j);
                        break;

                        case 2: if( ( (a[i]/10)==0 ) && ( (a[i]/100)==0) )
                                    return match(a, pattern, i+1, j+1);
                                return match(a, pattern, i+1, j);
                        break;

                        case 0: if ( ( (a[i]/10)==0 ) || (( (a[i]/10)==0 ) && ( (a[i]/100)==0)) )
                                    return match(a, pattern, i+1, j+1);
                                return match(a, pattern, i+1, j);
                        break;
                    }
                }

For some unknown reasonable reason, I get an error for the static method call although I ran it through another one...

Please help me make it complie as well...

Thank you all !!

Recommended Answers

All 33 Replies

For some unknown reasonable reason, I get an error for the static method call

this is very normal. in a static context, you don't always have an instance, so you can't just call an instance method.

public static boolean match(int[] a , int[] pattern)
{
//need to use the overloaded method, in order to work with the static method.
return match(a,pattern,0,0);
}

assume this:

public static void main(String[] args){
 int[] b = {3,4,5};
 int[] pat = {6,7,8};
 match(b, pat);
}

now, this first call is to a static method, which doesn't need an instance to call it through. the second one, however, if you try to call it from the main method, you also first have to create an instance of the class in order to call it through. the same concept goes here.

either make the method static, or call it through an instance of the class.

So how can I change my code in order to get a code with no errors ?
I choose:

return match(a,pattern,0,0);

In class, we always act this strategy in order to run a static method.
We took a regular method and called it through a static one, just to say that we got a static method running, kind of a trick...

How can I do the same here, without an errors ?

thx !

as I said: either you make the second method also static, or you create an instance of the class through which you call it.

if it is an instance method (which it is) you can only call it without an instance from within the instance itself. (it'll automatically take the method from the current instance).

but, static members, such as your first call, are bound to the class, not to a(ny) instance of it.

so, do something like this:

public static boolean match(int[] a , int[] pattern)
{
//need to use the overloaded method, in order to work with the static method.
NameOfContainingClass myInstance = new NameOfContainingClass();
return myInstance.match(a,pattern,0,0);
}

Is there another way than create an instance of my class ? the reason is to use this method as a portable method I can take with me to another class without change the instance type... ? thx

instance methods are linked to instances. they are not meant, nor supposed to be "portable". that kind of defeats the entire purpose of them being instance variables.

the closes to such 'portable' methods are static methods. but beware, sometimes they are coupled to the class they're in.

the method as you have, can be moved to another class, sure, but you would still need to create an instance of that class to make it. if you declare it as static, you would still need to call it through either (best practice) the class it's in, or through an instance of the class, even though that is not recommended, since developers reading your code mide mistake them for instance methods.

still gives me an error.. it says:
"The method match(int[], int[]) in the type Ex is not applicable for the arguments (int[], int[], int, int)"

The pattern here is a recursive method with a number of obscure parameters that are involved in controlling and tracking the recursion. In order to make it easier to use, there's a simpler public version without all the extra parameters, that simply calls the recursive method with a suitable set of initial parameters.

Note that there's nothing intrinsically static or non-static about that pattern, it just comes down to whether the methods need access to any instance members of the class.

match(int[] a, int[] pattern, i, j )
does not seem to use any instance members, so there's no reason for it to be an instance method. Indeed, there's a strong logical argument that says such a method should be static.
Just declare it static.

"The method match(int[], int[]) in the type Ex is not applicable for the arguments (int[], int[], int, int)"

looks like it's taking the wrong method.

Can you run this to try fix it ? I did tried everything and it wont work...

Could you try to change the private method name to something else? It is confusing.

Also, your condition looks odd and I don't think it works.
1) a[i]/10==0 ; where a[i]>=0 and a[i]<10 (note, a[i] is an int!)
2) a[i]/10==0 superceeds a[i]/100==0 because if a[i]/10==0 is true, there is no need to check for a[i]/100==0 (note, a[i] is an int!)
3) Your switch statement does not support other value, means return nothing if it goes through switch statement. However, the method must returns a value.

PS: You may need to add 'static' to your private method and force all arguments to be constant (final).

but in class we did the same (other Q, same idea...) and it works...
can you post a code suggestion ?

What the same idea? Post your code in class please?

Also, static method may not work well with primitive array argument (i.e. int[]) because it is mutable (static method is not supposed to do any changes/updates of any argument value). The call is unsafe because you are calling a non-static method from a static method. If you force argument to be a constant, it ensures that the argument will never be changed inside the method [private static boolean match(final int[] a, final int[] pattern, final int i, final int j)]. However, this is not the best way to solve the problem. The real problem is in your design.

private static boolean match(final int[] a, final int[] pattern, final int i, final int j)

didn't helped...

Can you fix my whole code so it will compile ? please... we never deal before with overriding a static method, it basically a stupidity, but what can I do, these are the requirements... can u help ? thx..

I did try with that change but with different method name, and it worked. It did compile and it ran without a problem. As I said, you are calling the "private" match method (match(int[], int[], int, int)) which is a non-static from your static method (match(int[], int[])). It should clarify your code if you name method differently when methods are NOT supposed to be polymorphism (in this case, the non-static and static should NOT be polymorphism).

PS: Show me how you call your methods (from anywhere) as well.

can you post the code you've changed ? please.....I really desperate about, because everything works except this call.... thanks...

Adami: if it worked in class, it would 've worked here just the same.
stop trying to look for new stuff, look at the code you had in class, and compare it to the code you have here.

you have been given quite some pointers as to where you change your code. don't ask for our updated code, since our code won't learn you anything. it's by you updating your code that you will figure out how to solve this. re-read the above posts. every change has been documented.

so, follow the above posts one by one, and you'll get there.

I have to retracked my statement "(static method is not supposed to do any changes/updates of any argument value)" because it is more on my practice -- may cause unexpected result to the caller. Thanks to JamesCherrill. ;) (Also, I am not certain that a static method is really a thread safe -- given an array variable and update it while other threads are using the same variable).

PS: Adami, did your updated code compile? If it does not compile, there is NO change in your .class file, which mean you will always get the same error. As I said before, show me 1) how you call your method and 2) the code that you said which is the 'same idea' you mentioned earlier.

Yes, static methods are no more thread safe than instance methods - ie not at all unless you synchronise them yourself.
I think the question of modifying the parameters may come down to whether this is a "real" object class (Employee. Bicycle etc), in which case an static method that modified any instance's data could be very "unexpected", or if it's a "service" class like Arrays or Collections which are just a holding area for static methods that definitely do modify their parameters, eg Arrays.sort(someArray).

Taywin: I wrote the following as for your recommendations:

public static boolean match(int[] a , int[] pattern)
    {
        return tryMatch(a,pattern,0,0); //need to use the overloaded method, in order to work with the static method. 
    }


    private boolean tryMatch(int[] a, int[] pattern, i, j )
    {

        /*stop condition: true if we've reached the end of pattern array, means we've got a match
         * false if we've reached the end of the inspected array, and still no full match*/
        if(j==pattern.length)
            return true;
        if( (i==a.length) && (j!=pattern.length) )
            return false;

        switch(pattern[j]){

            case 1: if( (a[i]/10)==0 )
                        return match(a, pattern, i+1, j+1);
                    return match(a, pattern, i+1, j);
            break;

            case 2: if( ( (a[i]/10)==0 ) && ( (a[i]/100)==0) )
                        return match(a, pattern, i+1, j+1);
                    return match(a, pattern, i+1, j);
            break;

            case 0: if ( ( (a[i]/10)==0 ) || (( (a[i]/10)==0 ) && ( (a[i]/100)==0)) )
                        return match(a, pattern, i+1, j+1);
                    return match(a, pattern, i+1, j);
            break;
        }
    }

But still I get the following errors:

return tryMatch(a,pattern,0,0);

gives: "The method tryMatch(int[], int[]) in the type Q2 is not applicable for the arguments (int[], int[], int, int)".

    private boolean tryMatch(int[] a, int[] pattern, i, j )

gives: Syntax error on token ",", delete this token.

It's driven me crazy, can't get why...

What am I doing wrong now ?

replace this:
private boolean tryMatch(int[] a, int[] pattern, i, j )
with
private boolean tryMatch(int[] a, int[] pattern, int i, int j )

you are calling this:

match(a, pattern, i+1, j+1);

several times, but match doesn't take those parameters, tryMatch does. replace match in those lines by tryMatch.

Because of the question's requirements, I needed to stay with overloading, so I wrote the code as following:

public static boolean match(int[] a , int[] pattern)
    {
        return match(a,pattern,0,0);    
    }


    private static boolean match(int[] a, int[] pattern, int i, int j )
    {

        if(j==pattern.length)
            return true;
        if( (i==a.length) && (j!=pattern.length) )
            return false;

        switch(pattern[j]){

            case 1: if( (a[i]/10)==0 )
                        return match(a, pattern, i+1, j+1);
                    return match(a, pattern, i+1, j);
            break;

            case 2: if( ( (a[i]/10)==0 ) && ( (a[i]/100)==0) )
                        return match(a, pattern, i+1, j+1);
                    return match(a, pattern, i+1, j);
            break;

            case 0: if ( ( (a[i]/10)==0 ) || (( (a[i]/10)==0 ) && ( (a[i]/100)==0)) )
                        return match(a, pattern, i+1, j+1);
                    return match(a, pattern, i+1, j);
            break;
        }
    }

Although now the errors I get are:
1. For each 'break' I get: "Unreachable code".
2. For

private static boolean match(int[] a, int[] pattern, int i, int j )

I get: "This method must return a result of type boolean".

I can assume there is a connection between them both, but I can't get which... can you help ?
Thx !

Your method is missing a return statement, a default one in case it never enters the if statements (Usually, the default return statement doesnt execute in applications that you always have one of the switch inputs, but is needed to satisfy the syntax)

acually, no. it contains to much return statements.

 case 1: if( (a[i]/10)==0 )
return match(a, pattern, i+1, j+1);
return match(a, pattern, i+1, j);
break;

here, there are two possible cases. either
if( (a[i]/10)==0 )
returns true, or it returns false.
if it returns true,
return match(a, pattern, i+1, j+1);
is executed.
if it doesn't return true,
return match(a, pattern, i+1, j);
is executed.

there is no possible scenario in which that break statement can be reached. just remove those break statements, and try again.

But if I'll remove the breaks, will I hurt the syntax of 'switch-case' statement ?

No, the break is optional in a switch statement. It's often omitted in code like

switch (something) {
  case 1: 
  case 2:
  case 3:  do something for cases 1, 2 and 3
  etc

So you're saying that the only thing I need to do is to eliminate those breaks ?

"Unreachable code"

You need to remove the breaks that come after a return, because executing the return means that the break that follows it can never be reached.

"This method must return a result of type boolean".

You also need to ensure that your code will return a boolean no matter what happens in the switch, including the situation where none of the cases is true.

Now there're no errors:

public static boolean match(int[] a , int[] pattern)
{
    return match(a,pattern,0,0);    
}
    private static boolean match(int[] a, int[] pattern, int i, int j )
    {
        if(j==pattern.length)
            return true;
        if( (i==a.length) && (j!=pattern.length) )
            return false;
        switch(pattern[j])
        {
            case 1: if( (a[i]/10)==0 )
                        return match(a, pattern, i+1, j+1);
                    return match(a, pattern, i+1, j);
            case 2: if( ( (a[i]/10)==0 ) && ( (a[i]/100)==0) )
                        return match(a, pattern, i+1, j+1);
                    return match(a, pattern, i+1, j);
            case 0: if ( ( (a[i]/10)==0 ) || (( (a[i]/10)==0 ) && ( (a[i]/100)==0)) )
                        return match(a, pattern, i+1, j+1);
                    return match(a, pattern, i+1, j);
        }
    return false;
    }

Do you think it's a good and efficient recursive method ?

The code looks neat, but I don't know what it's supposed to do, so I can'y comment on its efficiency or correctness. If it gives the right answers then it's almost certainly pretty good!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.