Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this code giving an "unreachable code" error?

I can't seem to find a way to fix this problem. All i'm doing is declaring an integer and it's telling me that the code is unreachable.

private class myStack{
    Object [] myStack = new Object[50];

    private void push(Object a){
        int count = 50;
        while(count>0){
            myStack[count]=myStack[count-1];
            count--;
        }
        myStack[0]=a;
    }

    private Object pop(){
        return myStack[0];
        int count2 = 0; //Unreachable Code
    }   
}
like image 947
user1078174 Avatar asked Dec 04 '22 17:12

user1078174


2 Answers

Once you return from a method, you return to the method that called the method in the first place. Any statements you place after a return would be meaningless, as that is code that you can't reach without seriously violating the program counter (may not be possible in Java).

like image 117
Makoto Avatar answered Jan 16 '23 09:01

Makoto


Quoting a comment on the question by Jim H.:

You returned from the pop() method. Anything after that is unreachable.

like image 43
jambriz Avatar answered Jan 16 '23 09:01

jambriz