This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In Java, what does a reference to Class.class do?
Firstly apologies for asking this question here. I know it's a simple question and can probably be answered quite quickly and easily. However searching for Java .class is such a loaded search term it brings up nothing useful.
Having said that, I just started learning Java and I just came across something like this:
Intent i = new Intent(this, ReminderManageActivity.class);
I am not sure, but I think ReminderManageActivity.class is an attribute that gives a string of the name of the class? Is that correct?
I know it sounds stupid, but class is a Class object that represent the Class.
If you want to instantiate / use some class, and you want to get this class as parametr, you can't just write Intent i = new Intent(this, ReminderManageActivity); since ReminderManageActivity is not an object.
In Java, given an object, an instance of a class, you can get the class name by coding the following:
Class clazz = obj.getClass();
String clazzName = clazz.getName();
Sometimes you want you want to create a Class object for a given class. In this case you can do so by writing code similar to the following example:
Class clazz = MyClass.class;
source
If you just started learning java than you will see more about this when you will study Type Information or RTTI(Runtime Type Information)
ReminderManageActivity.class
is called a class literal and it produces a reference to the Class object, which contains information about the class in question.
It is similar to the static method :
Class.forName("ReminderManageActivity");
It is an instance of java.lang.Class which is part of Java Refleciton API. You can get more informaiton about the API here
Because everything in Java is an object, so even classes can be represented by objects.
ReminderManageActivity.class returns a Class object representing the ReminderManageActivity class.
A Class object contains information of a class, such as class' name, class' method, class's variables etc. You can look at the class API for more information: http://download.oracle.com/javase/6/docs/api/java/lang/Class.html
Related
This question already has answers here:
Extend java.lang.String
(6 answers)
Closed 7 years ago.
I want to write a custom method for the String class so that I can call it like the following:
String s = "hello world";
s.myMethod();
Is their a way to accomplish specifically this?
If not then I will just do the following, but I would still like to know.
myMethod(s);
This cannot be done because you can't add methods to existing classes in Java.
It's even more interesting because the class String in Java is a final class, which cannot be subclassed.
You may be interested in this Stack Overflow answer because the thing you are describing is known as monkeypatching, which is common in more dynamic languages than Java. One of the answers links to cglib, one of several libraries that do some magic with the bytecode. Try hard enough and violate basic principles and you can do quite a bit, including making 5 be 3.
Update: This question and its answers seem to address your exact question. Some of the answers are pretty creative.
It is not possible. The best aproach in java would be create your own class that extends from string and write there your method. This way your class would be like string class but including your stuff.
but is not possible because string is final. So create a class that contains just a string object and write ther your method refering to the string. So you can invoke string methods through your string object in your class, and invoke your method through your class directly
I'm currently learning to program on Android using the book Android Programming: The Big Nerd Ranch Guide, and I've encountered this line of code
Intent i = new Intent(getActivity(),CrimeActivity.class);
I can't seem to understand why the Intent's constructor need that second argument.
If my knowledge serves me right, classes in Java are only a blueprint of an object..
So I'm confused why a literal class is passed as an argument in the Intents constructor?
What's happening right there?
In Java, everything except primitive types, is an object. The class definitions we write are wrapped in an Object of Class class. For instance:
class Foo{
}
Foo.class is an instance of Class class.
Class objects hold the information about the class information, like: name, list of instance variables, list of methods etc.
This information can be used at runtime via reflection.
Documentation
According to official developers guide -
This provides a convenient way to create an intent that is intended to
execute a hard-coded class name, rather than relying on the system to
find an appropriate class for you.
You're right, that the class is something like a blueprint for an object. You give the Intent you create that "blueprint" because the Intent itself (resp. the Android system when finally serving your Intent) will create an instance (an object) of the class you passed to it.
That's the reason you pass just the class and not an instance to an Intent.
This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 9 years ago.
I am currently in the process of learning Java, and I have done what I could to try and understand what these two things mean and do, but I simply don't get it. I have done a lot of googling, and also checked around for past questions on the site, but I still don't get it. So far I know that "this" somehow refers to some kind of method/variable/object, but I just don't get how it can refer to any of these, without (in the examples I have seen) actually specifing what to refer to, they just use the keyword "this.something or this(532);", it does not make sense. Same story with toString, they is no logic in these two that I get. Anyways, can anyone explain these two keywords in a simple way to me, and tell me how they can be even remotely useful in any program. Thanks in advance.
this.something refers to object something in the current class.
this.method() refers to a method of the current class
this() refers to constructor of the current class
toString() is a method used to create String representation of an object. Since every class in Java is a sub-class of Object and Object has toString() method, every class has a default toString() method. Often you will need to override the default toString() in order to get meaningful results.
The this keyword refers to the instance of the class.
this(512) refers to a constructor of the class that takes an int as Argument
this.field refers to a field of the class
this.method() refers to a method of the class
The toString() method is inherited from Object but can be overridden in a subclass to get more meaningful output.
For example if your class is like the following:
public class Database {
public Database (int port){
}
}
Then to refer this class' object, you can use like so: this(3306);
As a result this keyword is used for referring to the class
The this keyword refers to the current instance of the class (see http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html).
The toString() keyword isn't a keyword: it is a method name. It is defined in the Object class and can be overridden. What it does is creates the text representation of the class (see http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString()).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is a class literal in Java?
I was going through literals in the Java tutorial where I came across this sentence:
Finally, there's also a special kind of literal called a class literal, formed by taking a type name and appending ".class"; for example, String.class. This refers to the object (of type Class) that represents the type itself.
Which doesn't make any sense to me, even though I paid attention to all the other topics prior to this. Can anyone explain in simple language with examples or references?
Instances of the class java.lang.Class represent classes and interfaces in a running Java application. For each class in the application, there is an instance of Class. The SomeClass.class syntax is a way to get from SomeClass to the corresponding instance of Class.
A class literal is just a special type to use when you want to do something involving the class itself, rather than an instance.
Here's a short list of a few things I commonly use this for (not at all comprehensive, but you can get a good idea)
1.) Reflection, you want to instantiate something in run-time that you may not know (perhaps it was stored in a variable of type Class)
2.) You want to check if 2 objects are of the same related type, you can write something along the lines of: B.class.isAssignableFrom(a.getClass());
3.) You want to list all the methods, or public variables within a class, perhaps for documentation.
There are many other uses, but these are the main ones I find myself using in common practice.
Speaking simple language: that thing, which you call class literal is an object which fully describes some class: all its methods, all its fields, all its annotations, class's modifiers and so on. It is needed for creating new instances of that class in runtime.
Short example:
Class x = String.class;
System.out.println(x);
you can use x to create runtime instances of the class it points to or to test the class of an object against it.
It evaluates to be the class identifier of the reference or primitive type's wrapper class. The expression void.class evaluates to the class identifier of the Void class. Same thing with 'String.class'
During my interview, interviewer asked me
Can we create class without name ?
Since, I was not sure, if it is really possible to create a class without name. So, I said No.
Later, I tried Googling and found, others are also looking for the answer of the same question, but I didn't found clear answer.
I will appreciate, if anyone clearly explain about this class. I mean, what that class technically known as and how can we instantiate this class ?
Yes, it's called an anonymous class/struct.
In C++:
class {
} x;
x is an object of the type, and you can't create any more, because, well, how would you, given that the class doesn't have a name and all....
how would one call constructor and destructors
You don't. In both Java and C++ constructors and destructors hold the same name as the class (they're not PHP - __construct or whatever), and the missing name kind of gets in the way.
Its also called an anonymous class in Java.
// create a new instance of an anonymous class.
Serializable s = new Serializable() {
};
Note: In the JVM, all classes have a name, it's generated by the compiler for you.
You can't define constructors, but it can have an instance initializer block which does much the same thing.
In java, you can create "anonymous inner classes", for a detailed answer see How are Anonymous (inner) classes used in Java?