这个例子得到String的类名,保存在className中
public class Test{
  public static void main(String[] args) {    String s1="<123<456";
    String tmp = s1.getClass().toString();
    String className = tmp.substring(tmp.lastIndexOf(".")+1);
    System.out.println(className);
    }
}

解决方案 »

  1.   

    import java.io.*;
    import java.util.*;public class Test{
      public static void main(String[] args) {    String s1="<123<456";
        String className = null;
        String tmp = s1.getClass().getName();
        if (tmp.indexOf(".") != -1)
            className = tmp.substring(tmp.lastIndexOf(".")+1);
        else
            className = tmp;    System.out.println(className);
        }
    }
      

  2.   


      public static void test(List s) {
        System.out.println("Testing " + 
          s.getClass().getName() + ");
          .....
    }
      public static void main(String[] args) {
         test(new ArrayList());
       }其中s.getClass().getName() 得到结果为java.util.ArrayList
      

  3.   

    抱歉各位,我可能没有说清楚。
    我想做一个类。比如叫A.A里面有个方法,可以自动获得调用A的类的类名。
    比如类B调用了A,A里面的方法就自动获得了B这个类名。
    程序的大概意思是:
    public class B{
      public static void main(String[] args) {    A a = new A();
        a.xxx();
        }
    }
    通过类A的方法xxx(),就自动获得了class B的类名称B.
    不知道,我说清楚了没有。
    请问大家,我该怎么做呢?
      

  4.   

    B b = new B();
    String name = A.getClassName(b);
    public class A{
      public static String getClassName(Object o){
        String className = null;
        String tmp = o.getClass().getName();
              if (tmp.indexOf(".") != -1)
                  className = tmp.substring(tmp.lastIndexOf(".")+1);
              else
                  className = tmp;    return className;
      }
      public static void main(String[] args) {    String s1="<123<456";
        System.out.println(getClassName(s1));
     }
    }
      

  5.   

    请调用 A.getClassName(getClass());public class A{
      public static String getClassName(Class c){
        String className = null;
        String tmp = c.getName();
              if (tmp.indexOf(".") != -1)
                  className = tmp.substring(tmp.lastIndexOf(".")+1);
              else
                  className = tmp;    return className;
      }
    }
      

  6.   

    to ilka() :
     可以直接用getClass()吗?不需要实例化吗?
      

  7.   

    to mercury1231(在N座大山压迫下) :
      你说容易,那又该怎么做呢?请说清楚。