Solved

how to get java class name

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This video teaches viewers about errors in exception handling.

707 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

13 Experts available now in Live!

Get 1:1 Help Now