3

In my main method, my first argument is a class PersonInfo lets say. How do I do this?

 if(argument.equals(PersonInfo) {
  //invoke method A
 }

  if(argument.equals(MyInfo) {
  //invoke method B
 }

Since arguments in main method are Strings, how do I check if these Strings equal my class name?

rickygrimes
  • 2,637
  • 9
  • 46
  • 69

2 Answers2

4

PersonInfo.class.getSimpleName() is what you are looking for, and do it otherway so that you won't have to handle null check

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Perfect. However, what happens when I do it the other way, and the argument the user passes is null? Do I need to do a null check there? – rickygrimes Jul 30 '14 at 20:42
  • This answers it http://stackoverflow.com/questions/5712100/interview-java-equals/5712112#5712112 – jmj Jul 30 '14 at 20:42
1

You want to get the name of the class then compare it to your argument.

if(argument.equals(PersonInfo.class.getSimpleName()))
{
  //invoke method A
}

if(argument.equals(MyInfo.class.getSimpleName()))
{
  //invoke method B
}
apxcode
  • 7,696
  • 7
  • 30
  • 41