0

I am trying to write a static method that returns a string of my computer's MAC address (the function itself was found here: http://www.mkyong.com/java/how-to-get-mac-address-in-java/). I am having issues with the return aspect of the static function. The error I get is the missing return statement. How do I remedy this?

static String returnMacAddress(){
        InetAddress ip;
        try{
            ip = InetAddress.getLocalHost();

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            byte[] mac = network.getHardwareAddress();

            System.out.print("Current MAC address: ");

            StringBuilder stringBuilder = new StringBuilder();
            for(int i = 0; i < mac.length; i++){
                stringBuilder.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            return stringBuilder.toString();
        }catch(UnknownHostException e){
            e.printStackTrace();
        } catch(SocketException e){
            e.printStackTrace();
        }
    }
1
  • 4
    What does the method return if you get an UnknownHostException or SocketException? Commented Oct 9, 2013 at 19:18

1 Answer 1

5

All branches must return something, just add a return null; at the end:

static String returnMacAddress(){             // 1.
    InetAddress ip;
    try{                                      // 2.
        ip = InetAddress.getLocalHost();      // 3. (until return stmt)

        NetworkInterface network = NetworkInterface.getByInetAddress(ip);
        byte[] mac = network.getHardwareAddress();

        System.out.print("Current MAC address: ");

        StringBuilder stringBuilder = new StringBuilder();
        for(int i = 0; i < mac.length; i++){
            stringBuilder.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
        }
        return stringBuilder.toString();       // 4.
    }catch(UnknownHostException e){            // 5.
        e.printStackTrace();                   // 6.
    } catch(SocketException e){
        e.printStackTrace();
    }
    return null;                               // 7.
}

This is syntactically correct - but you have to think about what this means semantically, and if that is the desired operation:

  • exceptions - do you really just want to print them on System.err?
    • do you want to print them at all, if you just need the address if it is valid?
  • would you rather have a String returned representing the condition that it was not successful ot obtain the MAC address?

EDIT How the control flows in this case - as OP asked if the return null at the end would negate the previous value, in a successful execution:

  • enter method - new stack frame (1. in code)
    • enter try block (2. in code)
      • process instructions in try (3. in code)
      • return statement: stop execution of block, the value is returned to the previous stack frame (4. in code)
      • (not a case now, but if there was a finally block, that would be executed now, and that could even overwrite the returned value...)
  • execution of the method that called continues with returned value

In unsuccessful case, (UnknownHostException for example):

  • enter method - new stack frame (1. in code)
    • enter try block (2. in code)
      • process instructions in try (3. in code)
      • exception thrown
    • enter catch block (5. in code)
      • process catch block (log exception, 6. in code)
      • (not a case now, but if there was a finally block, that would be executed now, and that could even overwrite the returned value...)
    • return null statement: stop execution of block, the null value is returned to the previous stack frame (7. in code)
  • execution of the method that called continues with returned value

As you see, in successful case, the return null; statement, even though it is after the "real return", doesn't influence the returned value. Whenever a return is reached, the eecution of the current block is stopped. (and if there is one in the actual context, the appropriate finally block will get the control).

The finally block is tricky though: read up on that, it will be useful knowledge.

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

7 Comments

Thank you very much good sir. Can you explain a bit to me the importance of return null? Would this invalidate my method by returning a null instead of the String I want?
I'd suggest making a variable String address = null; before the block, address = stringBuilder.toString(); inside, and return address; at the end.
@Kevin I thought of that (In fact, I think of such in 8 hours a day :) ), but my opinion it is better to see null directly. For me it immediately sends the signal "hey, this method can return null" without having to parse the whole thing.
I see your point, though I generally assume a method will return null if it encounters an error (maybe too much c programming).
@Kevin is there such as too much C? :) Jokes apart: it is just an opinion, a personal preference - we all have to code the way it benefits the group the most...
|

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.