0

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

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

can it possible ?

3
Contributors
2
Replies
6
Views
5 Years
Discussion Span
Last Post by Taywin
0

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

Edited by NormR1

0

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.

Edited by Taywin

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.