27

First time developing in Java, and first time developing for Android, so it's quite a newbie question.

I currently have this code:

public void onBtnClicked(View v){
    /** Handles button navigation */

    @SuppressWarnings("rawtypes")
    Class c;
    int viewId = v.getId();

    switch (viewId) {

    case R.id.btnNewTourny :
        c = NewTourny.class;
        break;
    case R.id.btnTeamEditor :
        c = EditTeam.class;
        break;
    case R.id.btnCatEditor :
        c = EditCat.class;
        break;
    case R.id.btnLoadTourny :
        c = EditCat.class;
        break;
    case R.id.btnNewCategory :
        c = NewCat.class;
        break;
    default :
        c = Main.class;
    }

    Intent myIntent = new Intent(v.getContext(), c);
    startActivityForResult(myIntent, 0);
}

Short question:

What does the .class property do, f.ex. in 'c = NewTourny.class'?

Why can't I cast c as Tourny (which is the parent of all these classes)?

Long question:

This currently handles all button navigations throughout my app, and works fine. However, as you can see, I've suppressed a 'rawtype' warning, that comes when I cast c as a Class . Now, I really want a code without warnings, so I thought 'well, all these classes are a child of 'Tourny'', so I thought it would work, casting c as Tourny - but that does not work. Then I realised, that I didn't really know what the code "NewTourny.class" actually did, and that may be the clue to why I couldn't cast c as Tourny. So, what does .class do?

LongInt
  • 1,739
  • 2
  • 16
  • 29
  • Related post - [What does .class mean in Java?](https://stackoverflow.com/q/15078935/465053) & [About the “class” property/field](https://stackoverflow.com/q/1072066/465053) – RBT Aug 03 '18 at 00:01
  • Another related post - [Java “.class” property - C# equivalent](https://stackoverflow.com/q/42228180/465053) – RBT Aug 03 '18 at 00:10

5 Answers5

14

It doesn't really do much. NewTourney.class is more of a language specific way to write "the object which is the Class of NewTourney". This shorthand allows one to refer to the class of a NewTourney, as opposed to dealing with specific already existing instances of NewTourney.

In your specific case, I would hazard to guess that the Activity eventually creates an instance of the class to handle the action, or at least to hold the action's context sensitive data.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
4

Class is a class that represents classes. :)

It is not "Object". It is not a superclass or even in your "NewTourny" hierarchy. That is why you can't cast from a NewTourney to a Class.

The .class property is an instance of Class that represents type information.

You can use it to do reflective style calls to instantiate something you don't know the type of at compile time.

horbags
  • 784
  • 5
  • 4
  • 10
    Actually, a `Class` is an `Object` which represents a _class_. Classes are Objects, which is one of the idiosyncratic necessities for manipulating a `Class` within a JVM, which only has two types of data, primitives and descendents of `Object`. – Edwin Buck Apr 24 '12 at 22:12
4

Instances of the class where Class represent classes and interfaces in a running Java application. This is part of java Reflection API. From Oracle tutorial:

If the type is available but there is no instance then it is possible to obtain a Class by appending ".class" to the name of the type. This is also the easiest way to obtain the Class for a primitive type.

Here are some references:

  1. https://docs.oracle.com/javase/tutorial/reflect/class/classNew.html
  2. https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38
1

To get rid of the warning, you can change Class c with Class<? extends Tourny>. This means c is not an object, that represents one of all possible classes, but more specifically represents a class derived by Tourny.

phlogratos
  • 13,234
  • 1
  • 32
  • 37
  • Since c can point to many different types of classes it should be declared Class>. – Michael Krussel Apr 09 '12 at 17:04
  • When changing it to Class c, I get this error in each line of my switch statement: _Type mismatch: cannot convert from Class to Class_ – LongInt Apr 09 '12 at 17:08
  • @ Michael Krussel: That did the job of removing the warning! Thanks. – LongInt Apr 09 '12 at 17:10
  • Sorry, it has to be `Class extends Tourny>` instead of `Class`. – phlogratos Apr 09 '12 at 17:16
  • 1
    `Class` represents *only* the `Tourny` class. `Class extends Tourny>` represents a subclass of Tourny. That's the way generics work. Check http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html. – Frans Jun 22 '12 at 09:54
1

The .class method is one all objets have which allow you to reference the original nature of that object from before you even had a chance to initialize it. It's not the parent, it's the original unadulterated template of itself. It's called ".class" because it's a direct representation of the class that only the class definition can influence. It saves you from having to make a new object in order to obtain information all members of that class originally have in common.

Michael
  • 41
  • 3