1

I have a method that i'm calling a lot:

Integer.parseInt(input);

Is there a way i can take this text and replace with a shorter key word like

parseInput

so that i could use it as follows

month = parseInput

I've been told that Java does not have #define like C does, but is there no other way at all?

2 Answers 2

4

The shortest thing I can think of to do would be to use the static import feature, which lets you use another class's static methods within your own class, provided you don't break any rules about which methods you can access. So if you write

import static java.lang.Integer.parseInt;

at the top of your file, you can then just write

month = parseInt(input);

when you want to use it. I can't imagine anything much shorter than that.

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

Comments

0

parseInt is static method of Integer class. You can import static classes in your class and use them.

import static java.lang.Integer.parseInt;

...

@Test
public void parseIntTest() throws Exception {
    String val = "10";
    int intVal = parseInt(val);
}

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.