Solved

statically calling getClass().getName()

Posted on 2002-07-18
8
1,571 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 38
Java JRE greater than 1.6 5 62
java jdbc batch example 8 32
eclipse console opening separately 2 18
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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