Results 1 to 10 of 10
Thread: Naming object instances
- 02-05-2008, 07:35 PM #1
Member
- Join Date
- Feb 2008
- Posts
- 4
- Rep Power
- 0
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:
Java 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"; }
Java Code:Customer john = new Customer(etc. etc)
ThanksLast edited by oldgit; 02-06-2008 at 04:12 AM.
- 02-06-2008, 05:32 AM #2
Of course(if I understood your question correctly..).
Let's take your existing code and add the necessary parameters. Original code:
Java 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"; } }
Java Code:public class Customer { // five-param constructor for a Customer public Customer( String [B]firstName, String lastName, String title, String email, String mobile[/B]){ this.firstName = [B]firstName[/B]; // notice the removal this.lastName = [B]lastName[/B]; // of the quotations " " this.title = [B]title[/B]; this.email = [B]email[/B]; this.mobile = [B]mobile[/B]; } } }
Java Code:... Customer c = new Customer("John", "Smith", "Developer", "foo@bar.com", "555-5555"); ...
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!
-CaptVote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-06-2008, 07:05 AM #3
Member
- Join Date
- Feb 2008
- Posts
- 4
- Rep Power
- 0
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
Java Code:Customer c = new Customer("John", "Smith", "Developer", "foo@bar.com", "555-5555");
Hope that's clearer
- 02-06-2008, 08:15 AM #4
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....
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-06-2008, 12:06 PM #5
Member
- Join Date
- Feb 2008
- Posts
- 4
- Rep Power
- 0
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-06-2008, 11:20 PM #6
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...
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",....);
Hopefully that was closer... People! - Chime in, more heads are better than one(or two). :)Last edited by CaptainMorgan; 02-06-2008 at 11:24 PM.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-07-2008, 11:58 AM #7
Named class
Originally Posted by 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. :D
I hope this helps. ;)Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 02-07-2008, 02:41 PM #8
Member
- Join Date
- Feb 2008
- Posts
- 4
- Rep Power
- 0
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, 03:23 PM #9
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. ;)Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 02-07-2008, 11:18 PM #10
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:
Java Code:Customer nick = new Customer(...); //Yielding the result of "nick" being the name in the customer object
Similar Threads
-
Removing all the instances of a given text form a String
By Java Tip in forum Java TipReplies: 1Last Post: 01-11-2008, 10:06 PM -
Naming a Class
By Java Tip in forum Java TipReplies: 0Last Post: 12-22-2007, 11:21 AM -
RMI naming problem
By Robbinz in forum New To JavaReplies: 1Last Post: 12-06-2007, 11:32 PM -
Naming conventions
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:53 AM -
error in JNDI naming
By jitendra.ibs in forum Java ServletReplies: 0Last Post: 06-08-2007, 05:23 PM
Bookmarks