sorry, i'm a newbie of java.
for example,

public static myObj createObj(String nameFormUser){
    myClass nameFromUser = new myClass();
    return nameFromUser;

can it possible ?

Recommended Answers

All 2 Replies

Variable names used in a program are created when you edit the program. If you want to assign a name to a value at execution time, use a Map<String, Value> where Value is the class to hold the value associated with the name (a String).

If you are talking about dynamically create an Object (class) in Java with user-defined name, you cannot do that. If you are talking about dynamically assign an attribute (or is being called as variable) of an object (class) with user-defined value, then it is possible.

// sample class object named 'myClass'
class myClass {
  String name;
  public myClass(String nameFromUser) {
    name = nameFromUser;

// a method that may be inside the myClass or other class
// the return object type must match
// modifier return_type method_name(method_argument, ...) {
//   ...statements...
// }
public myClass createObj(String nameFromUser){
  myClass objForUser = new myClass(nameFromUser);
  return objForUser;

I think you may need to read about what Java object and class are first. At the moment, you do not have an understanding of what object is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.