-1

I can't figure out why i have an error on this piece of code. Any suggestion?

Thanks

public class HelloWorld {

  public static void main(String[] args) {

    int choice = 2;

    choice.className().getName();

  }
}
aurel_lab
  • 149
  • 11

4 Answers4

3

You Cannot invoke className() on primitive type

hasnae
  • 2,137
  • 17
  • 21
2

You can't, because primitives are not objects.

If the fully-qualified name of a class is available, it is possible to get the corresponding Class using the static method Class.forName(). This cannot be used for primitive types.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
1

You can't determine primitive variable data type like that way. code should be like below.

public class HelloWorld {
  public static void main(String[] args) {
    int choice = 2;
    String type = ((Object)choice).getClass().getName();
  }
}
Mishan Madhupa
  • 421
  • 4
  • 5
0

You can't because it is of primitive type, but you can use Integer instead and to check if it belongs to specific object you can use

Object temp = 1;
if(temp  instanceof Integer){
 System.out.println("Integer Object");
}
Usman Nazir
  • 106
  • 1
  • 6