Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on the class Object  RSS feed

 
Pradeep Sivadasan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a query on the following program

class A{
public static void getNames()
{
System.out.println("inside getname..");
}

}
public class B{
public static void main(String args[])
{
Object obj = new A();
//obj.getClass().getName().getNames(); <-- this st not working
A.getNames();

}
}
This code works..But in the class B when i call the A.getNames() that prints the result.But when we call obj.getClass().getName().getNames(); it throws error. though when we print the obj.getClass().getName() it shows the class A!!! what is the reason for this error?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
obj.getClass() returns the Class object that represents the class A.
obj.getClass().getName() returns a String, the name of the class A.

Given that, I have no idea why you expected to be able to call getNames() on the result of calling getName() -- String has no method getNames(), right?

Now, if you meant to try obj.getClass(),getNames(), at least I could understand your though process. An object of type Class is a source of information about a particular class -- it's not an instance of the class it represents, and you can't invoke the methods of the represented class. So, for example, you can ask a Class object for the name of a class, for whether it's a public class or not, for a list of its methods, for a list of its fields, etc. But you can't invoke those methods or query those fields unless you have an instance of the class itself.
 
Liz Hundy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
obj.getClass() returns the Class object that represents the class A.


So when I write
A.getNames();
what is A?

---Liz
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Liz Hundy:


So when I write
A.getNames();
what is A?

---Liz


A is a class (from the original poster), and getNames() is a static method of that class.

"obj.getClass()" returns a class object which is used to represent the class -- which BTW, can also be done via "A.class".

Henry
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Liz, for asking that question, because it made me realize what the original poster was asking! He's wondering why, if you can call

A.getNames()

and obj.getClass().getName() returns "A", then why can't you call

obj.getClass().getName().getNames() .

He might ask "In both cases, you're calling the method on "A", right?"

But the answer is no, you're not. "A" is a String, while A is an identifier, a symbol that appears directly in the source. In general, a String can represent the name of a variable, class, or method, but that doesn't make it the same as the variable, class, or method -- anymore than you and the word "Pradeep" are the same entity. If I write "Pradeep" on a piece of paper, I wouldn't be able to ask it to sit in front of a computer and write a Java program, right?
 
Liz Hundy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid I'm still a bit confused.

A is an identifier - a reference to what?
Not the same thing as obj.getClass()?

---Liz
 
Pradeep Sivadasan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly Liz..Thats what iam wondering..when u call obj.getClass() it returns class A..That means it obj.getClass().getNames() seems to be similar to A.getNames().
If obj.getClass() returns a string then what Ernest said was right. But i think obj.getClass() does not return a string.
 
Harshil Mehta
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep,

Read answer goven by Ernest very carefully.

obj.getClass().getNames() is not equal to A.getNames().
you can not invoke method on any class, you can invoke on object of that class.

here obj.getClass() will give you a class object that has informations like name of class, details about modifiers etc. But that does not make it object of the class on which getClass method is invoked!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!