Originally Posted by
oldgit
Is there any simple way to pass the 'name' ie john to the constructor at the time of instantiation - as a parameter?
Of course(if I understood your question correctly..).
Let's take your existing code and add the necessary parameters. Original code:
public class Customer {
String firstName, lastName, title, email, mobile;
public Customer(){
this.firstName = "firstName";
this.lastName = "lastName";
this.title = "title";
this.email = "email";
this.mobile = "mobile";
}
}
and turn it into:
public class Customer {
// five-param constructor for a Customer
public Customer(
String firstName, String lastName, String title, String email, String mobile){
this.firstName = firstName; // notice the removal
this.lastName = lastName; // of the quotations " "
this.title = title;
this.email = email;
this.mobile = mobile;
}
}
}
And now to instantiate your class:
...
Customer c = new Customer("John", "Smith", "Developer", "foo@bar.com", "555-5555");
...
For more on this see
Sun's tutorial on constructors.
There's many ways to perform both the creation and instantiation of a class. I hope I answered your question correctly.
And welcome to the Java Forums

See you around!
-Capt