View Single Post
  #2 (permalink)  
Old 02-06-2008, 07:32 AM
CaptainMorgan's Avatar
CaptainMorgan CaptainMorgan is offline
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by oldgit View Post
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:
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:
Code:
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:
Code:
... 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
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Reply With Quote