Solved

how to get java class name

Posted on 2003-11-18
4
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

690 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