This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Dear Geeks, 1 Is it possible to dynamically create a class and instantiate an object of it in java ? My Headache is as follows... I have a Singleton Logger which would print a log message on the system console Logger log= Logger.getInstance(); log.log("going to enter rmi and corba modules"); (This would print "going to enter rmi and corba modules"); Thereafter I need to write code like ... Logger rmilog = Logger.getInstance("RMI"); rmilog.log("rmi registry created"); (Should print "RMI:rmi registry created") Logger corbalog = Logger.getInstance("CORBA"); corbalog.log("corba naming service initialised"); (Should print "CORBA:corba naming service initialised") ie I want the singleton Logger to get me an instance depending on the argument that I pass . ie I want to dynamically create 2 classes namely RMILogger(a singleton) and CORBALogger(a singleton) that would subclass Logger and override log method and would prefix the log messages with "RMI" and "CORBA" respectively. Thanks in advance YAMH ( Yet Another Muddle Head)
Thanks Grau, But what I was trying to do was to create a class dynamically ie say Class dosntExistOnDisk=new Class(); dosntExistOnDisk.addConstructor(..); dosntExistOnDisk.addMethod(...) or rather I was trying to create a Type Dynamically .which I have understood that its not possible . Yet Another Doubt has crept into my head.. I have three packages A,B,C and I have 20 classes in each of these packages. So thought of writing a logger.(<JDK 1.4 hence) Dint want 20 instances of logger to be created in each package hence I made it a singleton( Great I used a pattern). But the behaviour of logger would differ in each package. In package A it will log the Message,time,X,and Y...In package it will log only message and so on. I end up creating 3 singleton loggers for Package A,B and C. I see that my logger has 20 methods of which only one differs across A,B and C. Now The Big Question ? How Can I Abstract the behaviour of singleton objects.IS IT possible . DO I need to go for Aspect Oriented Programming ? [ Dont know what that means but aspectj.org speaks a lot] or am I really missing something inside my head Thanks in Advance Geeks -Mandan [ Any One out there from Kerala,India?]
No, Java doesn't allow you to do this (only Smalltalk allows internal access to the compiler that I know of). However, you can create a text file (a .java file) in your program, invoke the Java compiler on it (using Runtime.exec()) and then so long as the .class file is on your classpath, you can then use Class.forName() to load it. Once you've gotten that far, you're home free... Kyle
But what I was trying to do was to create a class dynamically ie say Class dosntExistOnDisk=new Class(); dosntExistOnDisk.addConstructor(..); dosntExistOnDisk.addMethod(...) or rather I was trying to create a Type Dynamically .which I have understood that its not possible .