Solved

how to get java class name

Posted on 2003-11-18
4
368 Views
Last Modified: 2012-08-14
I am inside the method of some object, let's say

class A
{
  void f()
  {
     

  }
}

then I create object and call method f()
(new A).f();
inside method f() how can I get classname of the object as string?
So the code to do so in this case should return "A"
0
Comment
Question by:amakalski
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9774777
String className = getClass().getName();
0
 

Author Comment

by:amakalski
ID: 9775805
Sorry, I forgot that method f() is static and I tried to use getClass().getName();
but compiler says that I cannot user non-static getClass() from within static method. Is there alternative for getClass() for static methods or in other words is it possible to get class descriptor from within static method without using 'ClassName'.class, because this static function I can move to another class without being tied to particular 'ClassName'
0
 

Assisted Solution

by:vaibhavmishra062201
vaibhavmishra062201 earned 25 total points
ID: 9776676
If you move this static method to another class, then it will be static method of that class, and it will print name of that class.
In any case, you will know the name of the class in which this static method resides.
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 50 total points
ID: 9776755
There is a way to find all the class names that you passed calling methods:
.......
         try
         {
            throw new Exception();
         }
         catch(Exception e)
         {
         StackTraceElement[] u = e.getStackTrace();

// Now u[0] describes the class you're in.

;JOOP!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question