| | |
making a string .. name of an object
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I want to input a string
Suppose the string was :
String s = "sani";
Now I want to make "sani" the name of an object of one of the class.
Suppose the name of the class is myClass.
I want to :
myClass sani = new myClass();
Any ideas??
Suppose the string was :
String s = "sani";
Now I want to make "sani" the name of an object of one of the class.
Suppose the name of the class is myClass.
I want to :
myClass sani = new myClass();
Any ideas??
Here I will create a class named StringClass and build constructors.....
Now I will create a class named myClass that also contains method main and call the value of s which is the string "sani"
If this is not what you were referring to let me know because I am unclear.
Java Syntax (Toggle Plain Text)
public class StringClass { public static String StringAsObject; // Defining the new Value of s when calling this method StringClass() { StringAsObject = "sani"; } // Building Constructors public static String getStringAsObject() { return StringAsObject; } public void setStringAsObject(String s) { StringAsObject = s; } }
Now I will create a class named myClass that also contains method main and call the value of s which is the string "sani"
Java Syntax (Toggle Plain Text)
public class myClass { public static void main(String [] args) { StringClass aString = new StringClass(); String s; s = aString.getStringAsObject(); StringClass.getStringAsObject(); System.out.println(s); } }
If this is not what you were referring to let me know because I am unclear.
Thanks Branderson
StringClass aString = new StringClass();
this is what you wrote but I want it as
StringClass sani = new StringClass();
Is it possible like that .... whenever I input a string it should become the name of an object.
StringClass aString = new StringClass();
this is what you wrote but I want it as
StringClass sani = new StringClass();
Is it possible like that .... whenever I input a string it should become the name of an object.
No it's not. The closest thing you'll be able to do is to use a HashMap. Which is a list of key/value pairs. The string would be the key, and the object would be the value.
Try that out, you may need to add some casts and stuff, but you should get the idea.
Java Syntax (Toggle Plain Text)
public class HashTest { private HashMap objList; private Object obj; private String objName; public HashTest(){ this(20); } public HashTest(int size){ this.objList = new HashMap(size); } public void add(Object obj, String name){ this.objList.put(obj,name); } public void remove(String name){ this.objList.remove(name); } public Object get(String name){ if(this.objList.containsKey(name)) this.objList.get(name); else return; } public void showObjects(){ String name; ArrayList ar = new ArrayList(this.objList.values()); for(int i = 0; i < ar.size(); i++){ name = ar.get(i); System.out.println(name); } } public static void main(String[] args){ HashTest test = new HashTest(5); String name = "sani"; Object o = new Object(); // <-- that would be some object you want to name String name2 = "mandy"; Object o2 = new Object(); test.add(o,name); test.add(o2,name2); test.showObjects(); test.remove("sani"); test.showObjects(); } }
![]() |
Similar Threads
- convert string into object (Visual Basic 4 / 5 / 6)
- String to Object (Java)
- Size of string object??? (C++)
Other Threads in the Java Forum
- Previous Thread: Tab error
- Next Thread: help creating objects for simulating cd collection
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






