Re: Class Literal ???
On 18 Mar, 00:42, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
On 3/17/2010 5:25 PM, TheBigPJ wrote:
Thanks all for the replies.
---Correct Syntax---
Class x = MyClass.class; //Right way of doing it
(Or rather adhering to the generic way of life:)
Class<MyClass> x = MyClass.class; //Right way of doing it
http://mindprod.com/jgloss/classforname.html
-'Uses of classForName'
http://www.codeguru.com/java/tij/tij0120.shtml
- 'The Class object'
---Validation of my understanding---
Your files:
a.class
b.class
c.class
abc.java
abc.class
newsgroup.java
newsgroup.class
Class abc{
public void method1 (String theName)
{
//TO DO: Insert if class exists check. try block etc..
Class<theName> theTempClass = theName.class;
}
Nope, you've still got the wrong idea.
The class literal is a compile time concept. You are mixing Strings and
runtime objects into the mix unintentially.
Class<MyClass> myClassObject = MyClass.class;
You can't use ".class" on ordinary strings. For that you will need:
Class<?> clazz = Class.forName(theName);> }
Class newsgroup{
public static void main(String[] args)
{
abc theTest = new abc();
theTest.method("a");
theTest.method("b");
//The General Idea
}
}
I think I get it now :)
If not please do comment and rip me to shreads :s
Shred'd
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>- Hide qu=
oted text -
- Show quoted text -
Ah yes Im only getting the String Class back there *Slaps head*.
---Validation of my understanding---
Your files:
a.class
b.class
c.class
abc.java
abc.class
newsgroup.java
newsgroup.class
Class abc{
public void method1 (String theName)
{
//TO DO: Insert if class exists check. try block etc..
Class<Class.forName(theName)> theTempClass = Class.forName(theName); //
or is it (Class.forName(theName)).class ?
}
}
Class newsgroup{
public static void main(String[] args)
{
abc theTest = new abc();
theTest.method("a");
theTest.method("b");
//The General Idea
}
}
---------
So a program is ran say Monday. Come Wednesday you need to load in
some modules. You have a feature inbuilt somewhat to like the above
that will allow you to load in more .class files? << Thats the idea?
But you are right why load up the above - when you can just create an
instance of the a/b/c class.
So metaphorically speaking a bus with people is a good one to use for
'class literal'. The bus is running and while it is running it will
load and unload people. Taking in to account the people can either get
off as they are finished or the bus can throw them off. And taking the
metaphor even further the bus can tell you where it was created from
etc..