String as a constructor name

How to pass a String as a constructor name in Java

Page 1 of 1

5 Replies - 639 Views - Last Post: 05 May 2010 - 06:22 AM Rate Topic: -----

#1 luke2003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-November 08

String as a constructor name

Posted 05 May 2010 - 03:24 AM

Hi,
I'm trying to simplify my code and create one generic method for a number of individual ones. They are pretty much the same except in each of them a differently named constructor is called.
maskTag52A(SwiftBlock4 b4, SwiftMessage m,
			String sampleIBANs[]) {
		List <Tag> b4tl = b4.getTags();
		if (b4.getTagValue("52A") == null)
			return;
		String[] tagvalues;
		tagvalues = b4.getTagValues("52A");
		for (int i = 0; i < b4.getTagCount("52A"); i++) {
			boolean IBANReplaced = false;
			Field52A fl = new Field52A(tagvalues[i]);

in this example I am calling Field52A constructor. I was thinking of building one method and pass a String into it. It would serve as a template for calling the constructor and methods of an object that has been created.
I have this...
genericMaskTagA(String tagA, SwiftBlock4 b4, SwiftMessage m,
			String sampleIBANs[]) {
		List <Tag> b4tl = b4.getTags();
		if (b4.getTagValue(tagA) == null)
			return;
		String[] tagvalues;
		tagvalues = b4.getTagValues(tagA);
		for (int i = 0; i < b4.getTagCount(tagA); i++) {
			boolean IBANReplaced = false;
			Field52A fl = new Field52A(tagvalues[i]);

however I do not know how to pass String tagA to a constructor eg. Field52A.
Field(tagA) does not work :(
Any ideas?

Regards,
Luke

Is This A Good Question/Topic? 0
  • +

Replies To: String as a constructor name

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: String as a constructor name

Posted 05 May 2010 - 03:37 AM

What you're describing is called a factory method. You could do something like this:

public class FieldFactory() {

  // I'm assuming Field is your superclass
  // If it's not just substitute your superclass
  public static Field createField(String tag, String tagValue) {
    if (tag.equals("52A")) {
       return new new Field52A(tagValue);
    } else if (tag.equals("something elsse")) {
       return new SomethingElse(tagValue);
    } else {
       // This line is important.
       // It will let you know straight away
       // if you pass an illegal value.
       throw new IllegalArgumentException(tag + " does not represent a valid class!")
    }
  }

}

This post has been edited by cfoley: 05 May 2010 - 03:38 AM

Was This Post Helpful? 2
  • +
  • -

#3 luke2003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-November 08

Re: String as a constructor name

Posted 05 May 2010 - 05:16 AM

thanks cfoley
I still don't understand how to use it though
I'll add this method to my class but how do I call it to create my constructor?
Sorry if it's a basic question.
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: String as a constructor name

Posted 05 May 2010 - 05:51 AM

consider this very simple example:
//an Animal interface
public interface Animal {
   void talk();
}

//Dog
public class Dog implements Animal{
   //constructor..
   public void talk(){
      //how dog talk
   }
}

//Cat
public class Cat implements Animal{
   //constructor..
   public void talk(){
      //how cat talk
   }
}

//The factory class
public class AnimalFactory {
   public static Animal getAnimal(String name){
        if(name.equals("Dog")){
             return new Dog();
        }
        else if(name.equals("Cat")){
             return new Cat();
        }
        //.... more animals
   }
}



since Dog and Cat are implementations of Animal, you can return a Dog or Cat in case of the inputted name String.
you can read more about the factory class:
http://www.javaworld...11-factory.html

This post has been edited by japanir: 05 May 2010 - 05:53 AM

Was This Post Helpful? 2
  • +
  • -

#5 luke2003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-November 08

Re: String as a constructor name

Posted 05 May 2010 - 06:19 AM

Thanks guys,
I figured it out
I've got it like this:
Field fl = FieldFactory.createField(tag, tagvalues[i]);

Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: String as a constructor name

Posted 05 May 2010 - 06:22 AM

That's it! Sorry, I should have included that in my example.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1