Find the Class Name in a static method

Hi All,
I am trying to find the class name inside the static main method. I want to write one main method that loads an instance of the class. Other folks have suggested tricks with the security manager or creating an Exception to look at the stack trace, but these methods don�t reflect the inheritance. I want SUBCLASSES to be able to run from the command line using the inherited main method.
public static void main(String args[]){
      JPanel thisJPanel = (JPanel) Class.forName(????).newInstance();
}Any Ideas

I want
SUBCLASSES to be able to run from the command line
using the inherited main method.Someone pointed this out already but more directly, static methods are not inherited.
The behavior you desire ca be achieved using the Factory pattern.
The idea of being able to subclass an application is a little bizarre. Why don't you just do something like this:
public static void main(String[] args){
    // check that there is at least one parameter 
    JPanel thisJPanel = (JPanel) Class.forName(args[0]).newInstance();
}

Similar Messages

  • Query to find the class name that has the maximum number of students ?

    I need the query to find the [class Name] which has the most number of students. please look below at the tables
    Students Table                                                                     
    StudentId     StudentName  ClassID                                    
    1                  xxx                   1                                           
    2                  yyy                   1                                           
    3                  zzz                   1  
    4                  fff                    2
    5                   ttt                  2
     Classes Table
     ClassID          ClassNane
     1                    CSHARP
     2                    JSHARP
    The result should be : CSHARP
    since there are 3 students in CSHARP and 2 students in JSHARP class
    Appreciate your help
    Thanks

    Try:
    DECLARE @Classes TABLE (
    ClassID INT identity(1, 1) PRIMARY KEY
    ,ClassName VARCHAR(50)
    INSERT INTO @Classes (ClassName)
    VALUES ('CSharp')
    ,('JSharp')
    DECLARE @Students TABLE (
    StudentID INT identity(1, 1) PRIMARY KEY
    ,StudentName VARCHAR(10)
    ,ClassID INT
    INSERT INTO @Students (
    StudentName
    ,ClassID
    VALUES (
    'xxx'
    ,1
    'yyy'
    ,1
    'zzz'
    ,1
    'fff'
    ,2
    'ttt'
    ,2
    SELECT TOP (1)
    WITH TIES C.ClassName
    FROM @Classes C
    INNER JOIN @Students S ON C.ClassID = S.ClassID
    GROUP BY C.ClassID
    ,C.ClassName
    ORDER BY COUNT(S.StudentID) DESC
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • How to find the class name of the  BO

    HI
      How to find out a class for a business object.
    For example for the Business Object FORMABSENC i want to find out the class name.
    iam not able to find out from se24 or swo1.
    Or please suggest me how can i use the method createbapistructure of this businessobject.
    Regards
    vijaya

    Hello Vijay
    When you double-click on "method" AbsenceForm.CreateBapiStructure (transaction SWO1) and switch to tab ABAP you see (on ERP 6.0) that the field Name is empty meaning this BO "method" has not yet been implemented.
    Other methods are implemented by modules of function group SWXA. Checking package SWX you will see that there are no suitable classes around for this object.
    Final remark: Methods of business objects has just a semantic meaning and has nothing to do with object orientation (i.e. classes).
    Regards
      Uwe

  • How to get the Class name of an API method?

    Like most of us, it's difficult to know where every method within a Java class is. Also, many of the same methods are scattered throughout Java Classes. I know of two methods that retreive this... getClass().getName(). But, you must have an object to use these. Is there a way to find out what Class contains a method or Class variable thru an Applet or Java Application?? Obviously, I can look at API Docs; but this takes time.
    Tks Randy

    RajEndiran wrote:
    Can anyone please let me know how we can get the class name of a page or region in oracle apex?What do you mean with class name? The name of the template (e.g. the css style class name)?
    I would also like to know how we get the DOM object ID for particular item.Use firebug or inspect the source code of the rendered page to see the object IDs. Other then then, the typical ID of page items is the name of the item. For regions you can set your own ID.

  • How to find the class name, the location from where it invoked a method

    Hi,
    I have a class A. The caller calls A.someMethod(), whenever this method is invoked, I want to find out the caller info, which class invoked this method, from where this class was loaded (may be the jar file name). Any help will be appreciated.
    Thanks.

    However since version 1.4 there is an easier way to extract that information from the Throwable:
    java.lang.Throwable
    public StackTraceElement[] getStackTrace()
    Provides programmatic access to the stack trace information printed by printStackTrace(). Returns an array of stack trace elements, each representing one stack frame.

  • Can we call a static method without mentioning the class name

    public class Stuff {
         public static final int MY_CONSTANT = 5;
         public static int doStuff(int x){ return (x++)*x;}
    import xcom.Stuff.*;
    import java.lang.System.out;
    class User {
       public static void main(String[] args){
       new User().go();
       void go(){out.println(doStuff(MY_CONSTANT));}
    }Will the above code compile?
    can be call a static method without mentioning the class name?

    Yes, why do it simply?
    pksingh79 wrote:
    call a static method without mentioning the class name?For a given value of   "without mentioning the class name".
        public static Object invokeStaticMethod(String className, String methodName, Object[] args) throws Exception {
            Class<?>[] types = new Class<?>[args.length];
            for(int i=0;i<args.length;++i) types[i] = args==null?Object.class:args[i].getClass();
    return Class.forName(className).getDeclaredMethod(methodName,types).invoke(null,args);

  • How to get the class name  static method which exists in the parent class

    Hi,
    How to know the name of the class or reference to instance of the class with in the main/static method
    in the below example
    class AbstA
    public static void main(String[] args)
    System.out.println(getXXClass().getName());
    public class A extends AbstA
    public class B extends AbstA
    on compile all the class and run of
    java A
    should print A as the name
    java B
    should print B as the name
    Are there any suggestions to know the class name in the static method, which is in the parent class.
    Regards,
    Raja Nagendra Kumar

    Well, there's a hack you can use, but if you think you need it,Could you let me the hack solution for this..
    you probably have a design flaw and/or a misunderstanding about how to use Java.)May be, but my needs seems to be very genuine..of not repeat the main method contents in every inherited class..
    The need we have is this
    I have the test cases inheriting from common base class.
    When the main method of the test class is run, it is supposed to find all other test cases, which belong to same package and subpackages and create a entire suite and run the entire suite.
    In the above need of the logic we wrote in the main method could handle any class provided it knows what is the child class from which this main is called.
    I applicate your inputs on a better way to design without replicating the code..
    In my view getClass() should have been static as the instance it returns is one for all its instances of that class.
    I know there are complications the way compiler handles static vars and methods.. May be there is a need for OO principals to advance..
    Regards,
    Raja Nagendra Kumar
    Edited by: rajanag on Jul 26, 2009 6:03 PM

  • Getting the class name from within the compiled class file

    hey!
    I was wondering (i know its possible) how to read a .class and get the name of the class. ok that made not much sense^^ so, You have a file: c:\f.class but it wont run because the file name has to be the same as the classes name, right? so how can you read the class file i guess in binary mode to receive the correct class name.
    I saw inside my f.class i have "realname.java" so i know now that the name of the class file should be realname.class, i tried ways to extract it from the class file but never had any success.
    i think i need to study english^^
    ps: i need this because i have some class files sent to me and the file names have been changed so they wont work, they only work when i open the class file in notepad and find out what its real name should be and then rename the file.
    Edited by: forgotmydamnpass on May 7, 2009 11:23 AM

    ah looks interesting, problem is i cant use it.. im prone to errors!
    import java.net.URLClassLoader;
    public class NewMain {
         * @param args the command line arguments
        public static void main(String[] args) {
            FileClassLoader loader = new FileClassLoader();
            Class clazz = loader.createclass("c:\\f.class");
            Method method = clazz.getName();
    }apparently "createclass" doesnt exist :/
    FileClassLoader = cannot find symbol
    Method = cannot find symbol

  • How to find out class name

    Hi folks,
    Can anybody help me, I got stuck on this problem.
    I need to find out the className of the class, which is specified
    by ordinary filename.
    In other words:
    User gives me filename (something.class) and I need to find out
    classname (I just need to create an instance of this class)
    Thanks a lot.
    sgtPepper

    This will read the class name from a class file.
    * @author Ian Schneider
    public class JavaClassName {
        public static void main(String[] args) throws Exception {
            DataInputStream din = new DataInputStream(new FileInputStream(args[0]));
            int cafebabe = din.readInt();
            if (cafebabe != 0xcafebabe)
                throw new Exception("Not class file");
            din.skip(4);
            int cnt = din.readShort() - 1;
            Object[] info = new Object[cnt];
            for (int i = 0; i < cnt; i++) {
                int b = din.readByte();
                switch (b) {
                    case 1:
                        info[i] = din.readUTF(); break;
                    case 3: case 4: case 9: case 10: case 11: case 12:
                        din.skip(4); break;
                    case 5: case 6:
                        i++;
                        din.skip(8); break;
                    case 7: case 8:
                        info[i] = Short.valueOf(din.readShort()); break;
                    default:
                        assert false;
            din.skip(2);
            int off = din.readShort();
            Number ptr = (Number) info[off - 1];
            System.out.println(info[ptr.intValue() - 1]);
            din.close();
    }

  • Access to Class information from a static method

    There does not appear to be anyway to access the Class object from instead a static method.
    e.g. How could I convert the following code so that it works ?
    class test {
    static void printClassName() {
    System.out.println( getClass().getName() );

    The idea was to have a static method that could be
    used to make a lookup based on the classname. Being
    able to access the class name would mean that I would
    not have to reimplement for every subclass !!!!!Hmm.. what are you trying to look up? Some kind of class-specific constant information maybe?
    In general your best bet is to define an abstract method in your parent class for return of the constant value, then define a specific method in each sub-class that overrides it, retrurning the appropriate value.
    Then you're common processing just gets the value with that method.
    Using some kind of lookup on the basis of class name is much clumsier, and more inclined to run time errors. Better to use the built in virtual methods table.
    Another approach is to have the value as a parameter of the constructor of the parent class and use "super(value)" in the child classes to store
    it.

  • From which table I can find the "Class type" and "Class" of the material?

    From which table I can find the "Class type" and "Class" of the material?
    Thanks in advance for the answers....

    Hi,
    try following table
    KSSK     Material number to class     
    KLAS     Class description     
    KSML     Characteristic name     
    CABN/CABNT     Characteristic name description     
    CAWN/CAWNT     Characteristic name
    [http://www.sap-img.com/materials/classification-view-of-material-master.htm]
    [http://wiki.sdn.sap.com/wiki/display/ERPLO/FrequentlyUsedTables]
    Regards
    kailas Ugale

  • How to know the class name in .ser file?

    Hi,
    I have a .ser file, could any expert help me to find out all the classes in the .ser file? Thanks!

    If you have written them as Object, then you can just read them one by one with an ObjectInputStream and when you have the object, you can print out the class name:
    Object o = in.readObject();
    if (o != null)
      System.out.println(o.getClass().getName());
    else
      System.out.println(null);

  • How to i get the class name ? please help me

    hi friends
    i want to know how to get the class name from my java program
    public class sample
    public static void main(String args[])
    how to i get sample.
    please help me.
    thanks in advance                                                                                                                                                                                                                                                                                                                                                                                                                                           

    I can't recall which forum member gave the following solution, but it works
    in static contexts and it's copy-and-paste friendly -- you don't hard-code the
    name of the class, only a dummy class that you use to make this hack work:
    public class Foo {
        private static class Dummy{}
        public static void main(String[] args) {
            Class cls = Dummy.class.getEnclosingClass();
            String name = cls.getName();
            System.out.println(name);
    }

  • Cannot find the class file for com.sap.portal.services.api.connectorgateway

    Hi,
    I was trying the "How to BI JAVA SDK in a Portal iView and get the following error.
    The project was not built since its classpath is incomplete. Cannot find the class file for com.sap.portal.services.api.connectorgateway.IConnectorGatewayService. Fix the classpath and try rebuilding this project.
    I have the following in my portalapp.xml file
    <property name="SharingReference" value="com.sap.portal.ivs.connectorservice"/>
    and i have also added the jar file "com.sap.portal.ivs.connectorservice_api" in the set of lib's
    Has some faced a similar issue? or can someone suggest how I could possibly resolve this.
    Thanks,
    Smitha

    Hi Smitha:
       My webdynpro dc was build failed for "This compilation unit indirectly references the missing type com.sap.engine.services.webservices.espbase.client.dynamic.content.GenericObject (typically some required class file is referencing a type outside the classpath)".
    How should i do?  Thank you!

  • How to find the Class Type of a Class ?

    Hi,
    Can anyone tell how can I find a Class Type of a Class?
    e.g. I know the Class name as "Z_ABC" but do not know its Class Type, therefore, not able to display it in CL01.
    Regards,
    Lucky

    Hi,
    Just by entering the Class name in CL02 does not display the data because system asks to enter the Class Type as well.
    Rammohan - Thanks for thissolution however, when I executed after entering the Class Name, table displyed no entries found..... message but this class is being used in the PO Release Strategy. May be the data is being saved in some other table. Is there any other way to find this?
    Regards.

Maybe you are looking for