1

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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.
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.
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.
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.
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.