-1

I tried to display by finding the string but i am not able to print the class name.this is my java code.i need to display class names like crosscutting themes,flag,log,basetheme,register,assign.

            class Theme {

                 }
          class crosscuttingtheme extends Theme{ 


            }
           class flag extends crosscuttingtheme{
                 public flag()
                        {
                    }
                    }
               class log extends crosscuttingtheme{

                      public log()
                         {
                          }
                                       }

                       class basetheme extends Theme
                                                { 


                                                        } 
                         class register extends basetheme
                              {
                       public register()
                               {
                                     }
                             }
                       class unregister extends basetheme{
                 public unregister()
                  {
                       }
                       }
                 class assignmarks extends basetheme{
                   public assignmarks()
                  {
                   }
                 }
sindhu
  • 1
  • 2

3 Answers3

0
System.out.println(getClass().getName());
animuson
  • 53,861
  • 28
  • 137
  • 147
Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37
  • it is giving only the main class name – sindhu Nov 26 '15 at 04:57
  • Post what your main looks like. Every object has a getClass() method. You should be able to take what I showed you and get what you need. If you still can't then post your main code and I'll see if I can guide you further. – Josh Chappelle Nov 26 '15 at 05:00
  • 'public class theme { public static void main(String[] args) { } } – sindhu Nov 26 '15 at 05:06
  • Your main does nothing. What are you trying to do? You don't have a reference to an object to print the class name. Is this a class assignment or something? Please give a little more information so I will know how to answer. – Josh Chappelle Nov 26 '15 at 05:31
  • i have to read this file using another code and display the class names..this is for my project – sindhu Nov 26 '15 at 05:32
  • So the code you posted above is actually a file that you have to read and output the classnames? – Josh Chappelle Nov 26 '15 at 05:36
  • You should try to write some code yourself and when you have a problem, post what your code looks like and what you don't understand. What you are doing right now is posting a blank main method with no work done and a problem to solve. I understand that it can seem overwhelming. Try to break it into sub problems. Smaller problems that make up the whole. Then try to solve each smaller problem and if you get stuck post your issue on here. – Josh Chappelle Nov 26 '15 at 06:15
0

checkout https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html

void printClassName(Object obj) {
     System.out.println("The class of " + obj +
                        " is " + obj.getClass().getName());
 }
pwilmot
  • 586
  • 2
  • 8
0

To print out the name of any class, you just have to get an instance of that class, then call .getClass().getName() on it. For example, for your Theme class, you could do as follows:

Theme theme = new Theme();
System.out.println(theme.getClass().getName());

This will print out the name of the class, 'Theme';

Bimde
  • 722
  • 8
  • 20