1

I want to do something like this:

Creator method = new Creator();
method.addSubject("example");

class Creator{
  public void addSubject(String subjName) {
     //here is the issue
     subjName = new Subject(subjName);
  }
}

class Subject {
  private String name;
  public Subject(String newName) {
    name = newName;
  }
}

So I want this class called Creator to be able to make Subjects, but I need it to be able to do so by passing it a String with the name that I want to call those subjects. How can I do this?

Edit: To clarify, the class "Creator" has a method called "addSubject". In the main method of the program I have an object of Creator called "method" (probably should have chosen a better example name). So can this object of Creator make objects of another class, class "Subject", simply by passing the method "addSubject" the name I want those objects of Subject to have?

Edit2: This is the pseudocode of what I want:

Main method:
Initialize Creator object
Command line for program takes arguments
Pass these arguments to creator object

Creator Object:
Takes command line argument in the form of string and makes a new object of the class Subject by the name of the String
5
  • I didn't understand well... Do you want to choose the class name during runtime? Commented Jul 24, 2012 at 1:51
  • You talking about creating a Class whose name is passed by a String? Commented Jul 24, 2012 at 1:51
  • It seems that your question is not clear enough. Your question refers to reflection, but your code is not consistent with that. Commented Jul 24, 2012 at 2:07
  • davidbuzatto, yes. The program takes an input via command line and creates this objects on the fly. Jon Lin, I am talking about creating objects of the class Subject whose name is passed by a String. Kumar, yes, I guess it is. Commented Jul 24, 2012 at 2:08
  • @Archer: I understood now. Take a look in my answer. ;) Commented Jul 24, 2012 at 2:12

2 Answers 2

4

I think you want to create a new object of a class that you just want to use the name. Is it? So, you can do this (Java 7).

try {
    // you need to provide the default constructor!
    Object newInstance = Class.forName( "your.package.YourClassName" ).newInstance();
} catch ( ClassNotFoundException | IllegalAccessException | InstantiationException exc ) {
    exc.printStackTrace();
}

If you are using a Java version prior to 7, you need to use 3 catch statements, one for ClassNotFoundException, one for IllegalAccessException and one for InstantiationException.

Edit: I think I understood now. You want to create instances of Subject with a name passed to the method. You can use a HashMap to simulate this.

Something like:

import java.util.*;

class Creator{

  private Map<String, Subject> map = new HashMap<String, Subject>();

  public void addSubject(String subjName) {
     map.put( subjName, new Subject(subjName) );
  }

  public Subject getSubject(String subjName) {
     return map.get(subjName);
  }
}

class Subject {
  private String name;
    public Subject(String newName) {
      name = newName;
    }
    @Override
    public String toString() {
      return name;
    }
}

// using...
Creator method = new Creator();
method.addSubject("example");

// prints example
System.out.println( method.getSubject("example") );

// prints null, since there is not a value associeted to the "foo" 
// key in the map. the map key is your "instance name".
System.out.println( method.getSubject("foo") );
Sign up to request clarification or add additional context in comments.

4 Comments

@Jeffrey: Ok, now I understood what he wants.
What does this do? @Override public String toString() { return name; }
When you pass an object as a parameter in a method argument that expects a String, the compiler changes your code (implicitly) to calls the toString method of that object. toString is a method inherited from Object class, so EVERY class in Java contains this method. It is used to create a String representation of the instance. In the code, I just overrided this method. The annotation Override should be used to have certain that the method was properly overriden. If you commit some mistake during writing the method and it is annotated with @Override, the compiler will warn you with an error.
Take a look in these links: JavaDoc and a simple tutorial
1

This is the bit that doesn't work:

subjName = new Subject(subjName);

subjName is a string, but of course a new Subject() is a Subject

How about

Subject myNewSubject = new Subject(subjName);

Of course, I imagine what you really want is to deliver that Subject somewhere (to a Collection maybe?) but your question doesn't clarify so I'll leave it at that.

1 Comment

But if I use the line "Subject myNewSubject = new Subject(subjName);", then won't every instantiated object of Subject have the name "myNewSubject"?

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.