dcsimg
Using getClass method of Object class in a factory pattern
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sundar_manyapu
Posted On:   Wednesday, July 24, 2002 10:54 AM

Dear friends, In a method i try called as shown below. public void test(){ ClassEx1 c = (ClassEx1)ClassEx1.getClass().newInstance(); } While compiling i am getting the following error at the command. ClassEx.java:14: non-static method getClass() cannot be referenced from a static context ClassEx1 c = (ClassEx1)ClassEx1.getClass().newInstance(); ^ 1 error Kindly let me know what could be the problem. Regards, Sundar    More>>

Dear friends,



In a method i try called as shown below.

public void test(){

ClassEx1 c = (ClassEx1)ClassEx1.getClass().newInstance();

}


While compiling i am getting the following error at the command.

ClassEx.java:14: non-static method getClass() cannot be referenced from a static context

ClassEx1 c = (ClassEx1)ClassEx1.getClass().newInstance();

^
1 error



Kindly let me know what could be the problem.

Regards,
Sundar

   <<Less

Re: Using getClass method of Object class in a factory pattern

Posted By:   jeremie_granat  
Posted On:   Wednesday, August 7, 2002 05:33 AM

getClass is a non-static method and can only be used with an object.



ClassName.class should be used if you don't have an object.

Re: Using getClass method of Object class in a factory pattern

Posted By:   Steven_Martin  
Posted On:   Wednesday, July 24, 2002 11:54 AM

Try it using :


ClassEx1.class.newInstance()
About | Sitemap | Contact