Passing Inner class name as parameter

Hi,
How i can pass inner class name as parameter which is used to create object of inner class in the receiving method (class.formane(className))
Hope somebody can help me.
Thanks in advance.
Prem

No, because an inner class can never have a constructor that doesn't take any arguments.
Without going through reflection, you always need an instance of the outer class to instantiate the inner class. Internally this instance is passed as a parameter to the inner class's constructor. So to create an instance of an inner class through reflection you need to get the appropriate constructor and call its newInstance method. Here's a complete example:import java.lang.reflect.Constructor;
class Outer {
    class Inner {
    public static void main(String[] args) throws Exception{
        Class c = Class.forName("Outer$Inner");
        Constructor cnstrctr = c.getDeclaredConstructor(new Class[] {Outer.class});
        Outer o = new Outer();
        Inner i = (Inner) cnstrctr.newInstance(new Object[]{o});
        System.out.println(i);
}

Similar Messages

  • Trying to pass a class as a parameter

    I am converting code written by someone else in VB6.  They had a lot of global variables and I am trying to find a way to use a class as a parameter so I can pass it to multiple forms.  I can’t get the method to accept the class.
    Here is the class definition:
        public class CommonCode
            public class gArrayContainer
                public gArrayContainer()
                    dateTimeInputRecord = null;
                    strPanelData = null;
                    strTimeSelected = null;
                    strPanelPresent = null;
                public gArrayContainer(int intNumberOfInputRecords)
                    dateTimeInputRecord = new DateTime[intNumberOfInputRecords];
                    strPanelData = new string[intNumberOfInputRecords, 4, 25, 8, 17, 12];
                    strTimeSelected = new string[intNumberOfInputRecords];
                    strPanelPresent = new string[intNumberOfInputRecords, 4, 25];
                public DateTime[] dateTimeInputRecord { get;set; }
                public string[,,,,,] strPanelData { get; set; }
                public string[] strTimeSelected { get; set; }
                public string[,,] strPanelPresent { get; set; }
                public string strTest {get; set;}
    This is where I am calling from:
    private
    void openBarButtonItem_ItemClick(object
    sender, DevExpress.XtraBars.ItemClickEventArgs e)
    try
    CommonCode cmn =
    new
    CommonCode();
                    CommonCode.gArrayContainer
    gArrayContainerInstance = new
    CommonCode.gArrayContainer();
                    cmn.mnuFileOpen(CallMe, gArrayContainerInstance);
    catch (Exception
    exc)
    Console.WriteLine(exc);
    throw exc;
    And this is the method I’m trying to call:
    public
    void mnuFileOpen(PassStatusBackFunction
    messageCallback, gArrayContainerInstance )
    try
    string strStartDirectory =
    string strDataPath =
    string strDataFile =
    string strDataFileTitle =
    int intCount = 0;
                    additional logic……
    catch (Exception
    exc)
                    Console.WriteLine(exc);
    throw exc;
    How am I supposed to code the class in the calling location and the definition?
    Thanks.

    Also (not sure why you've chosen c#) it would be far easier for you to convert from VB6 to VB.Net than it would be to C#, I even think theres some pretty robust converters knocking around the tinterweb

  • How to pass a Class as a parameter of a method?

    I have a method like below. Inside this method I need to create a variable that his type is the type of the class I have passed as a method's parameter.
    For example, if I call the method like this "Meth1(ClassPerson);" insed the method I need to declare a  variable of class "ClassPerson", but if I call like "Meth1(ClassCity);" I need the same variable to be declared as a ClassCity.
    public function Meth1( /*I dont know what to put here*/   ):void{
         /*I dont know what to put here to declare my variable*/
         /*here
         comes
         more
         code
         using
         the
         variable*/
    I tried as below but dont worked:
    public function Meth1( classtype:Class):void{
         var var1:classtype = new classtype();
    Can someone help me?

    evyatarBH, after declare the variable like you said an error occurs when I try to access some property of the variable. I receive a mesage telling that this property dont exists.
    public function Meth1( classtype:Class ):void{
         var var1:* = new ClassFactory(classe);
         //when I try to do something like the line below the error appear
         inputtext1.text = var1.fieldname;
    rashare, I can't do this because I can't declare the object before the method is called. I must inform to the method only which class I will use, but the object must be created only inside the method.

  • Hw To Make Inner Class Distinguish Func Parameter And Outer Class Var

    i fail to make an inner class distinguish among function parameter and outer class variable.
    consider the following code:
    public class a {
         int var; // i want to access this one using inner class.
         public a(int var) {
              this.var = var;
              class b {
                   public void fun() {
                        var = 100;
    i get an error a.java:9: local variable var is accessed from within inner class; needs to be declared final.
    when i change the code to this:
    public class a {
         int var; // i want to access this one using inner class.
         public a() {          
              class b {
                   public void fun() {
                        var = 100;
    it compiled no problem.it seems that inner class is accessing function parameter rather than outer class member.
    is there any syntax i can use to make me access the outer class member variable whithout renaming or removing the function parameter?
    thank you.

    a.this.var = 100; //Use this in the inner class.Amazing syntax -:)

  • Non fully qualified class names in parameter list

    Hi Folks,
    When I generate my documentation the fully qualified class names (for each parameter) appear in the parameter list for methods. In looking at the documentation on this site, a hyperlink to the class using the only the class name appears. Can anyone provide thoughts on how this is achieved? Thanks.

    The -link and -linkoffline options enable the documents to link to other API documentation
    generated in separate runs of javadoc. Linkoffline is usually the one you want.
    http://java.sun.com/j2se/1.4/docs/tooldocs/win32/javadoc.html#linkoffline
    With 1.4.0, you can also choose to omit the package name even if -link or -linkoffline
    is not used by using -noqualifier.
    -Doug Kramer
    Javadoc team

  • Pass in file name as parameter

    Can canyone help me in how to pass the filename as parameter? I have a list of xml files so i hope to pass the files. Does anyone have a code sample of showing me how to do? Thanx

    i have a java program that is to print out the xml files such as Declaration.xml, Aviation.xml and Financial.xml so i hope to create a class that is to pass in these file names to this program.

  • How to pass entire class as a parameter in a method of other class- OpenScr

    Hi Folks,
    Whenever we create a script in OpenScript, it can contain only one class. However I want to declare several variables in a class/script and pass this entire class B as a parameter in a methodA of classA.
    What I mean by mean that is , I create ClassB something like below
    public class MyParams.....
    variable1;
    variable2;
    variable3;
    variable4;
    variable5;
    public class Original.....
    public void methodA(MyParams params)
    params = new MyParams();
    field1 = params.variable1;
    field2 = params.variable2;
    field3 = params.variable3;
    field5= params.variable5;
    Edited by: OATS Explorer on Mar 8, 2012 1:53 AM

    Hi
    I have a stand alone program (can be used by all other users) which will accept request_id and To_Email as parameters. I am developing a new request set in which I want to incldue this stand alone program and want to pass Prog#1 request Id to the 2nd stand alone program as a parameter so that I can send the output of the Prog#1 to the users through E-mail.
    Do we have any options to pass Prog#1 requeset id in to Prog#2 as a parameter in the request set without modifying the stand alone concurrent program code.
    Please note that i dont want to change stand alone program code becuase this code/program was developed to work in general for all the users and I dont want to change this code for new requirement/purpose...
    Hope you understood my requirement...
    Thanks!!

  • Issues to pass a column name as parameter. please advice.

    hi all,
    i have some difficulty with oracle reports. i'm a novice with 2 days beginner knowledge.
    i am not able to use lexical parameters in reports. my query is simple like
    select &col_name abc from xyz;
    i have tried making a parameter before creating the SQL but still i recieve the error saying that ORA-00922: missing expression.
    also i have tried with bind variables like
    select :col_name abc from xyz;
    but it gives a :col_name inplace of the records data.
    could some help me with step by step procedure for creating a lexical parameter.
    i am using reports 6i.
    please let me know for any details required.
    thanks in advance!!!

    When I create a lexical, I simply type in the SQL:
    select
    &lp_column_name
    from table_x;Save the sql, then the lexical parameter will be automatically created. After that you can specify what lp_column_name means, by specifying its value in the Before Report trigger. You'll probably have something like this in the PL/SQL editor:
    begin
      if :p_column_name is not null then
        :lp_column_name := :p_column_name;
      end if;
    end;where :p_column_name is one of the input parameters when user runs a report in Oracle Applications.

  • Passing innerclass name as parameter

    Hi,
    How i can pass inner class name as parameter which is used to create object of inner class in the receiving method (class.formane(className))
    Hope somebody can help me.
    Thanks in advance.
    Prem

    Make the inner class as a parameter. An outer class object would also be required to create an inner class object.
    OuterClass outerClass=new OuterClass();
    InnerClass innerClass=outerClass.new InnerClass();

  • Is it possible to pass a string representing a class name in java as an arg

    Hi, this is probably a bit of a stupid question, but one that has me quite confused all the same!
    Is it possible to pass a string or class name etc. representing a type of class in java, to a method so that instead of having to redefine a method with say the following args:
    public SolarPanels[] bestPVPanels(int budget, int percent) {
            HashMap<Integer, SolarPanels> panelsMap    = new HashMap<Integer, SolarPanels>();
    }As at present needing to create a methods bestWindTurbine() and many others exactly the same, but for the type, I would instead like to be able to create a method like:
    public Object[] bestRenewable(int budget, int percent, String aClassName) {
            HashMap<Integer, aClassName > renewableMap    = new HashMap<Integer, aClassName >();
    }But cant sus how to do this as passing a String is off course going to cause problems unless its it possible to cast a string to a class name, any help or advance would be much appreciated.
    Thanks in advance
    Pat Nevin

    pNev wrote:
    But cant sus how to do this as passing a String is off course going to cause problems unless its it possible to cast a string to a class nameIt's not. You can do things like
    Class.forName(classNameInString);But that will only return a Class object.
    And as Java generics are erased at runtime, passing the class name to the method is useless as it is too late to use it anyways.
    Ah, Peter's way is what I was thinking of. Too early in the morning.
    Edited by: Kayaman on 23.6.2010 10:02

  • Passing Block Name As Parameter

    Dear Friends
    I have procedure to search for specific data, and the results will be shows on different data blocks, based on the button called the procedure, i passed the block name as parameter to the procedure, but when i want to set the values of the items in the data block, i got error, i used the following code
    copy(name_in(items_rec),block1||'.item_name')
    each time i got the following error: ORA-01722.
    can you please help me in this problem
    and thanks in advance for all friends
    Mohammad

    Hi,
    Try
    copy(name_in(items_rec),name_in(block1||'.item_name'))I assume, block1 is the variable which you are passing as a parameter to the procedure.
    Regards,
    Arun

  • Help , inner class included.

    hello all :
    I want to check a java file , which include inner class or not , and I want to get the inner class name of it , how can I do it ?
    looks like this :
    java InnerClassChecker Mother.java
    InnerClassA
    InnerClassB
    InnerClassC
    thank u.

    I would suggest compiling Mother.java and then you can do this with the Class object of the mother class.
    Class c = motherObj.getClass();Then you can call c.getClasses(). This will return all public inner Classes of Mother.
    If you dont have the mother Object and want to do it with the parameter it would look like this:
    java InnerClassChecker Mother.class
    Then your InnerClassChecker takes the <param> in its main method and calls
    Class.forName(<param>) to load the Mother class object.

  • Doubt in Inner class

    hi ,
    i have a doubt. How we can access a Inner class name by the outer class name using the Dot(.) operator .
    For example if we want to refer to the Inner class in some other class
    then we must say "OuterClass.InnerClass".
    This type of syntex is ment for the static methods in a class.
    Thus can we assume that Inner class is also a type of static member.
    If not then please explain the syntex.
    i am also not getting the way we create object of the Inner class object in some other class . (i mean to say the logic behind the syntex)
    i.e
    OuterClass Ob1=new OuterClass();
    OuterClass.InnerClass InnOb=Ob1.new InnerClass();
    Cna any one please help me out.
    Regards
    Arunabh

    Hi,
    Inner classes are used to create some functionality in terms of OO way. Like you want to have event handling which will work only for designated GUI classes, then we can make use of Innner class.
    Within a outer classes we do not require any reference of outer object BUT, if you want to access Inner class object outside the class then only we need a reference of Outer object so that we instantitate Inner class object.
    thanks,
    Sandeep

  • JLS3: Scope of type parameter and inner class declaration

    The following code:
    class Foo<E>
      class E
        E foo()
          return this;
    }produces this error message:
    $ javac Foo.java
    Foo.java:7: incompatible types
    found   : Foo<E>.E
    required: E
          return this;
                 ^
    1 errorApparently, the type parameter E of the outer class Foo shadows the inner class declaration E. An alternative way to check is to create a new E() in this method, which results in an error message about using a type parameter where a type is expected.
    Of course this code is sick, but I would like to know where this is defined in the JLS3, since I cannot find out in which way this shadowing works. What I've found until now:
    Page 179:
    "The scope of a class' type parameter is the entire declaration of the class."
    Page 190:
    "The scope of a declaration of a member m declared in or inherited by a class type C is the entire body of C, ... "
    Page 132:
    "If a type name consists of a single Identifier, then the identifier must occur in the scope of exactly one visible declaration of type with this name, or a compile time error occurs."
    Section 6.3.1 (Shadowing declarations) specifies the shadowing rules, but does not mention type parameters.
    Any help is appreciated.

    The current behaviour in eclilpse seems to be the one we get if the bug in javac is fixed as described.
    This code compiles
    public class Shadow1 {
         class Foo<E>
           class E
              E foo()
               return this;
    }but this code
            Foo<E>.E foo()
               return this;
             }shows
    Type mismatch: cannot convert from Shadow1.Foo<E>.E to Shadow1.Foo<Shadow1.Foo<E>.E>. E
    Not sure if this error message is correct.

  • How to pass Method Inner Class reference to other method?

    Hi All,
    I am trying to pass the reference of "method inner class".
    Can any one explain me how to pass the reference and where other method will sit in the class, I mean either in Outer Class or in Inner Class ?
    Thanks in advance for ur reply :)
    package methosInnerClass;
    public class MethodLocalInnerClass {
         private String outerName;
         private static String statOuterName;
         public MethodLocalInnerClass(String name, String statName) {
              outerName = name;
              statOuterName = statName;
         public void methodWithLocallClass() {
              class MethodInnerClass {
                   String innerName;
                   MethodInnerClass(String name) {
                        innerName = name;
                   public void displayOuterInner() {
                        System.out.println("Outer Name: " + outerName + "\nOuter StatName: " + MethodLocalInnerClass.statOuterName + "\nInner Name: " + innerName);
              MethodInnerClass methodInner = new MethodInnerClass("Harish");
              methodInner.displayOuterInner();
                    *Pass above reference to other method*
         public static void staticMethodWithLocallClass() {
              class MethodInnerClass {
                   String innerName;
                   MethodInnerClass(String name) {
                        innerName = name;
                   public void displayOuterInner() {
                        // We can not access the non-static instance variable since this method is a static method
                        //System.out.println("Outer Name: " + outerName + "\nOuter StatName: " + MethodLocalInnerClass.statOuterName + "\nInner Name: " + innerName);
                        System.out.println("Outer StatName: " + MethodLocalInnerClass.statOuterName + "\nInner Name: " + innerName);
              new MethodInnerClass("Shakshi").displayOuterInner();
         public static void main(String[] args) {
              new MethodLocalInnerClass("Abhishek","Neeshu").methodWithLocallClass();
              System.out.println("Calling innerClass within static method !!!");
              staticMethodWithLocallClass();
    }

    package donald.test.inner_class;
    public class OutterClass {
         private String outerName;
         private final OutterClass outterClass;
         public OutterClass() {
              outterClass = this;
         public void methodWithInnerClass(final String strValueToPassToInnerClass) {
              class InnerClass {
                   private InnerClass innerClass;
                   private String innerName;
                   InnerClass(String name) {
                        innerName = name;
                   public void displayOuterInner() {
                        System.out.println("Non-Static:\tOuter Name: " + outerName + "\tInner Name: " + innerName);
                        System.out.println("");
                        System.out.println("final String strValueToPassToInnerClass = " + strValueToPassToInnerClass);
                    * @return the innerClass
                   public InnerClass getInnerClass() {
                        return innerClass;
                    * @param innerClass the innerClass to set
                   public void setInnerClass(InnerClass innerClass) {
                        this.innerClass = innerClass;
                    * @return the innerName
                   public String getInnerName() {
                        return innerName;
                    * @param innerName the innerName to set
                   public void setInnerName(String innerName) {
                        this.innerName = innerName;
              InnerClass methodInner = new InnerClass("Inner.Donald");
              methodInner.displayOuterInner();
              System.out.println("My OutterClass " + outterClass.getOuterName());
        // Unknown "MethodInnerClass "
         public void passReferenceOfInnerClassToOtherMethod(     ) {
          * @param args
         public static void main(String[] args) {
              OutterClass outterClass = new OutterClass();
              outterClass.setOuterName("Outter.Donald");
              outterClass.methodWithInnerClass("This Donald is so very cool...!!!  Yeah...!!!");
          * @return the outerName
         public String getOuterName() {
              return outerName;
          * @param outerName the outerName to set
         public void setOuterName(String outerName) {
              this.outerName = outerName;
          * @return the outterClass
         public OutterClass getOutterClass() {
              return outterClass;
    }

Maybe you are looking for