Solved

statically calling getClass().getName()

Posted on 2002-07-18
8
1,581 Views
Last Modified: 2012-06-22
I'm just curious ... I don't need to do this for any reason, but ...

Is it possible to call the getName() method statically for a given class?

public class MyClass
{
    public static void main( String[] args )
    {
        System.out.println( getClass().getName() );
    }
}


ofcourse, the above example produces a compiler error because there is no instatiated "this" object from which to call the method (the method itself is not static).  Is it possible to get the current classname in this manner (from main() -- other than println("MyClass") ?
0
Comment
Question by:knightEknight
[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
8 Comments
 

Expert Comment

by:fi76631
ID: 7163222
No. You do not know the type of a class when it is static, only when it is constructed. Consider your class implementing an interface etc.....
0
 
LVL 33

Author Comment

by:knightEknight
ID: 7163239
I suspected as much ... thanks.
0
 
LVL 33

Author Comment

by:knightEknight
ID: 7163249
I'm trying to accept your comment as answer, but it won't let me ...
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 33

Author Comment

by:knightEknight
ID: 7163250
please lock it ... maybe that will work.
0
 
LVL 1

Accepted Solution

by:
klf earned 50 total points
ID: 7163276

try

public class MyClass
{
   public static void main( String[] args )
   {
       System.out.println( MyClass.class.getName() );
   }
}
0
 
LVL 33

Author Comment

by:knightEknight
ID: 7164640
as fate would have it ... it works!

ofcourse, you have to _know_ the classname in order to _get_ the classname ... so I don't know how much value this has.  But it does return the fully qualified package name, i.e.  com.test.MyClass  ... for what its worth.

Thanks to both of you.
0
 

Expert Comment

by:roo_j
ID: 8204843
try

public class MyClass
{
   public static void main( String[] args )
   {
       System.out.println( this.getClass().getName() );
   }
}
0
 
LVL 33

Author Comment

by:knightEknight
ID: 8206162
no because "this" has no object reference in the main().  But this would work:


public class MyClass
{
   public static void main( String[] args )
   {
      MyClass mc = new MyClass();
      System.out.println( mc.getClass().getName() );
   }
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

751 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