Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a '.class' property work?

Tags:

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?

like image 942
LongInt Avatar asked Apr 09 '12 16:04

LongInt


People also ask

What are class properties?

The collection of properties assigned to a class defines the class. A class can have multiple properties. For example, objects classified as computers have the following properties: Hardware ID, Manufacturer, Model, and Serial Number.

How do you create a classroom property?

You can create a property by calling property() with an appropriate set of arguments and assigning its return value to a class attribute. All the arguments to property() are optional. However, you typically provide at least a setter function.

What is a class property in Python?

Class Properties The property() method in Python provides an interface to instance attributes. It encapsulates instance attributes and provides a property, same as Java and C#. The property() method takes the get, set and delete methods as arguments and returns an object of the property class.

What does @property mean in Python?

The @property is a built-in decorator for the property() function in Python. It is used to give "special" functionality to certain methods to make them act as getters, setters, or deleters when we define properties in a class.


2 Answers

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.

like image 166
Edwin Buck Avatar answered Sep 24 '22 03:09

Edwin Buck


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.

like image 42
horbags Avatar answered Sep 21 '22 03:09

horbags