0

How to assign an instance to the value of a variable at the time of creation?

so I have a variable Name that is a list String[] Name= "Steve" , "prince" I want to assign Name[0] to be the name of the instance I have a class Person so id instantiate like so Person Name[0]= new Person (Name[0], Name[1]); but it throws me an error. here's my code so you can see it all. https://code.sololearn.com/c1p1XI2k2qv8/?ref=app

18th Feb 2018, 2:25 PM
StealthyTaco
StealthyTaco - avatar
3 Réponses
+ 6
You create an instance of Person and call the constructor which takes two parameters and assign them to firstname and lastname. Person obj = new Person(name[0], name[1]); You can then retrieve and manipulate the instance via your getters and setters, e.g. System.out.print(obj.getName());
18th Feb 2018, 2:33 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
You can't use a constant, variable, object, etc as an identifier. An identifier must be unique and only represent that one particular class, variable, method (with the exception of overloading), etc within its available scope, so as not to create a possible ambiguity or naming collision. Do as @Hatsy suggests and identify the object you're working with by getting its name property.
18th Feb 2018, 4:17 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
You can't do that. You cant really mix the coding interface with the actual code in that way. @Hatsy's answer is what you need to do.
18th Feb 2018, 4:42 PM
Zeke Williams
Zeke Williams - avatar