Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Class.forName("LL")

 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class Stat...then what is the problem in
try{
Stat s=(Stat)Class.forName("Stat");
}catch(Exception e){
System.out.println(e.getMessage());
}

It is giving compilation error
C:\test\Test.java:24: inconvertible types
found : java.lang.Class
required: Stat
Stat s=(Stat)Class.forName("Stat");
^
1 error
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName just returns a reference to the java.lang.Class type of the Class that get loaded, not an instance of it. If your class has a no-arg constructor, you can call Class.forName("").newInstance(), otherwise you'll have to find a refenece to the specific constructor and call it using reflection.
Dave
 
Mathews P Srampikal
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sir......
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't 'sir' me
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if Stat is in a package, Class.forName() will expect the fully qualified class name, e.g. "mkpkg.mysubpkg.Stat".
 
You can't have everything. Where would you put it?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic