Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-05-2008, 08:35 PM
Member
 
Join Date: Feb 2008
Posts: 4
oldgit is on a distinguished road
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:
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

Last edited by oldgit : 02-06-2008 at 05:12 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-06-2008, 06:32 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
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)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-06-2008, 08:05 AM
Member
 
Join Date: Feb 2008
Posts: 4
oldgit is on a distinguished road
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
Code:
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-06-2008, 09:15 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
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
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
Code:
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)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-06-2008, 01:06 PM
Member
 
Join Date: Feb 2008
Posts: 4
oldgit is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-07-2008, 12:20 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
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
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.

Quote:
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.

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-07-2008, 12:58 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Named class
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-07-2008, 03:41 PM
Member
 
Join Date: Feb 2008
Posts: 4
oldgit is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 02-07-2008, 04:23 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 02-08-2008, 12:18 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
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:
Code:
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing all the instances of a given text form a String Java Tip Java Tips 1 01-11-2008 11:06 PM
Naming a Class Java Tip Java Tips 0 12-22-2007 12:21 PM
RMI naming problem Robbinz New To Java 1 12-07-2007 12:32 AM
Naming conventions Java Tip Java Tips 0 12-03-2007 10:53 AM
error in JNDI naming jitendra.ibs Java Servlet 0 06-08-2007 06:23 PM


All times are GMT +3. The time now is 10:20 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org