views:

78

answers:

2

I have 2 questions that I was hoping someone could help me with. Is there a way to create a class on the fly with android/java and also add variables to the class? For example I would like to do something like this:

Class c = new Class();
c.name = 'testing';
c.count = 0;

c.getName = new function(){
  return c.name;
}

Just wondering if this is possible or if there is another way to do this. Basically I want to build an object that I can use the data from as an object.

A: 

This is not typically done. It can be done by reflection, but would be a fairly bad idea--This type of code is really annoying to debug, won't interact correctly in the IDE (For instance, ctrl-clicking on an instance of c.getName wouldn't be able to jump to where the method is defined), it would probably be a pretty big performance hit, etc.

However, for some generic tools this is possible. I believe Hibernate might have the ability to create classes from DB tables.

The most common use, however, is in mocking used within testing frameworks--They can do almost exactly what you want. Look at EasyMock with TestNG.

In general, though, you are better off just defining a business class and going with it rather than trying to make some abstract framework that generates your classes for you.

Bill K
+2  A: 

No, the syntax you describe is not possible in Java. I'm not sure what you are trying to accomplish there. If you want to create a class to use to hold data on the fly, you can create an anoynmous inner class.

Object object = new Object() {
  private String name = testing;
  private int count = 0;

  public String getName() {
    return name;
  }
}

In general, I wouldn't use this for a data objects though. This functionality is typically used for anonymous implementations of interfaces to support callbacks, etc.

Mayra
Thanks for the reply. I think that is what I am looking for. Is there a way to dynamically name the variables. For instance in php its called variable variables and you could do something like: $test = 'testing'; $$test = 'what'; Then if you echo $testing you would get 'what'. So you can name the variable based on a string basically.
ngreenwood6
No, thats not possible. Java is not a scripting language. You should not try to make it act like a scripting language. By doing so, you will lose all of the power of Java. Why do you want to do that? If you explain what you are trying to accomplish, we might be able to help you find the Java way of doing it.
Mayra
Well I am trying to write an xml parser using the sax parser that will create a class that I can easily parse independent of any type of xml file. This way I can just call that to parse the file and then work with the data independently.
ngreenwood6
I see... did you consider using a dom parser instead? The resulting dom model is essentially a generic java object tree that you can query to get data specific things by name, etc.
Mayra
I considered it but from what I was reading the dom parser is considerably slower than the sax parser. Thats why I wanted to create my own implementation.
ngreenwood6
Its more expensive than sax parser because it is loading the entire xml file into memory, while the sax parser is designed to only look at a small part of the xml at a time. However, it sounds like what you want is the entire xml file in memory to mess around with. I doubt you'll be able to come up with anything that is significantly more efficient than the current dom parser implementation to parse generic xml. The savings in using a sax parser typically come because you know the xml format and parse to an optomized object, but that doesn't sound like what you are doing.
Mayra