Solved

statically calling getClass().getName()

Posted on 2002-07-18
8
1,577 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
hibernate insert example 13 34
how to install java on RHEL image on EC2 4 41
How to determine if a string is a valid SHA value 7 35
Java: anonymous class 4 29
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

830 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