|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

02-05-2008, 08:35 PM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 4
|
|
|
Naming object instances
Hi all,
There is probably a simple answer to this and I'm going to kick myself, but...
How can I name an object instance? For instance, I am developing a Customer database and wish to add Customers details. So, I define a Customer class and methods to add customer details as:
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";
}
then in my 'Controller' class, I create a new Customer...
Customer john = new Customer(etc. etc)
Is there any simple way to pass the 'name' ie john to the constructor at the time of instantiation - as a parameter?
Thanks
Last edited by oldgit : 02-06-2008 at 05:12 AM.
|
|

02-06-2008, 06:32 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
|
|
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
__________________
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)
|
|

02-06-2008, 08:05 AM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 4
|
|
Thanks, Cap'n. But, maybe, I didn't explain clearly. I spotted the mistake with the quotations - I had edited the code after posting in the hope of adding clarity, but probably confused the issue!
What I'm really after is being able to define the 'c' as in
Customer c = new Customer("John", "Smith", "Developer", "foo@bar.com", "555-5555");
so that the name of the object is defined at the time of instantiation.
Hope that's clearer
|
|

02-06-2008, 09:15 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
|
|
Originally Posted by oldgit
Thanks, Cap'n. But, maybe, I didn't explain clearly. I spotted the mistake with the quotations - I had edited the code after posting in the hope of adding clarity, but probably confused the issue!
What I'm really after is being able to define the 'c' as in
Customer c = new Customer("John", "Smith", "Developer", "foo@bar.com", "555-5555");
so that the name of the object is defined at the time of instantiation.
Hope that's clearer
Maybe someone can weigh in on this... but I'm having difficulty understanding your question... what do you mean by "naming the object" ? You've repeated it twice without much difference... Technically, the above code shows a variable c which is a reference to a Customer object - which is instantiated with the values above... I'm not sure where "naming" is related or how you mean it specifically- in Java we deal with references, not necessarily names(but I suppose you could informally call 'c' a name of an Customer object). As far as definitions, the above is how we define/declare an object of the Customer class....
__________________
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)
|
|

02-06-2008, 01:06 PM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 4
|
|
|
Not doing very well at this, am I! Now, I think what I'm trying to ask is 'How can I define the name the reference at run time?'
Indeed, I am thinking of 'c' as a name for that object. So, if I wish to create a variable (in the example, c) is it possible to define the 'c' part. To give an example...
I would like to create a Student class and would like to define each instance with a name/reference. The name would be passes at run time - something like
Student Mohammed = new Student("Mohammed",....);
that is, deriving the instance reference from the class parameters. Is this any closer?
Thanks.
|
|

02-07-2008, 12:20 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
|
|
Originally Posted by oldgit
Not doing very well at this, am I! Now, I think what I'm trying to ask is 'How can I define the name the reference at run time?'
No problem, likely my fault too.
Indeed, I am thinking of 'c' as a name for that object. So, if I wish to create a variable (in the example, c) is it possible to define the 'c' part. To give an example...
Technically we are defining the "'c' part" in the code above.
I would like to create a Student class and would like to define each instance with a name/reference. The name would be passes at run time - something like
Student Mohammed = new Student("Mohammed",....);
Sure, you can still instantiate it with values that are not known until run-time. The question becomes, how are you going to be retrieving those unknown values? Through console input? Through a class import? Ultimately, where are the values coming from? But naming the reference according to it's parameters, I think, is a bit more involved... to be honest, I've never actually performed this - but the Java API contains numerous classes which make for the retrieval and manipulation of your instantiated classes easier.
Hopefully that was closer... People! - Chime in, more heads are better than one(or two). 
__________________
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)
Last edited by CaptainMorgan : 02-07-2008 at 12:24 AM.
|
|

02-07-2008, 12:58 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
Named class
Originally Posted by oldgit
Hi all,
There is probably a simple answer to this and I'm going to kick myself, but...
How can I name an object instance? For instance, I am developing a Customer database and wish to add Customers details. So, I define a Customer class and methods to add customer details as:
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"; }
then in my 'Controller' class, I create a new Customer...
Code:
Customer john = new Customer(etc. etc)
Is there any simple way to pass the 'name' ie john to the constructor at the time of instantiation - as a parameter?
Thanks
Hello oldgit
From your first post, I understand that you want the name of a instance of a class so that you can use it later. I don't know if this is possible in Java to get the "name" of an instance, but I know that you can create a system to emulate this effect. The idea that I have, is to create an abstract class, called "Named", that has a "name" attribute that is initialized by the constructor. This class should have a static data structure that is to reference all the instances created by the constructor. The Named class should also override the finalize() method, when an instance is collected by the garbage collector, so that it can remove that instance from the static list of instances. So, in affect, the Named class will manage a library of it's instances. Finally you can add a static search method to return the instance with a certain name. Letting Customer be derived from the Named class, you can then find any of its instances with a search method. If you want me to code if for you, I would gladly, but only if it will aid you.
I hope this helps. 
__________________
If your ship has not come in yet then build a lighthouse.
|
|

02-07-2008, 03:41 PM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 4
|
|
Thanks, Tim. I think I'm getting a little closer.
I think I've probably been spoiled by doing most of my coding using BlueJ, where you can give each instance a a name as it's created - as the image attached.
Thanks, again.
|
|

02-07-2008, 04:23 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
Java coding
Hello oldgit.
When learning Java, you must do it in a basic text editor. Using programs like these hide the important issues of creating Java code. I recommend that you complete your projects in a text editor first, and when you feel ready, you can start using an IDE.
Good luck. 
__________________
If your ship has not come in yet then build a lighthouse.
|
|

02-08-2008, 12:18 AM
|
 |
Senior Member
|
|
Join Date: Nov 2007
Location: New York
Posts: 143
|
|
Yeah, BlueJ is an environment in which you can graphically call a method. This removes you from the principal of object instantiation in a client program (AKA, testing the class in the main method). You should use something where you can't do this. Either notepad, like tim said, or another IDE that's simple, such as JCreator.
As for your problem. I think i understand what you're asking. Basically, if im correct, youre asking if the name of the variable can be used as a String in the argument of the class.
Ex:
Customer nick = new Customer(...);
//Yielding the result of "nick" being the name in the customer object
I actually used to wonder this myself. I'm not sure if it's possible, maybe someone else will know.
__________________
//Haha javac, can't see me now, can ya?
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|