Solved

how to get java class name

Posted on 2003-11-18
4
345 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now