Results 1 to 10 of 10
  1. #1
    oldgit is offline Member
    Join Date
    Feb 2008
    Posts
    4
    Rep Power
    0

    Default 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";        
           
        }
    then in my 'Controller' class, I create a new Customer...
    Java 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.

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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:
    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";        
           
        }
     }
    and turn it into:
    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];        
        }
           
        }
      }
    And now to instantiate your class:
    Java 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
    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)

  3. #3
    oldgit is offline Member
    Join Date
    Feb 2008
    Posts
    4
    Rep Power
    0

    Default

    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");
    so that the name of the object is defined at the time of instantiation.

    Hope that's clearer

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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
    Java 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....
    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)

  5. #5
    oldgit is offline Member
    Join Date
    Feb 2008
    Posts
    4
    Rep Power
    0

    Default

    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.

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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.

    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). :)
    Last edited by CaptainMorgan; 02-07-2008 at 12:24 AM.
    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)

  7. #7
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default 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. :D

    I hope this helps. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  8. #8
    oldgit is offline Member
    Join Date
    Feb 2008
    Posts
    4
    Rep Power
    0

    Default

    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.

  9. #9
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default 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.

  10. #10
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    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
    I actually used to wonder this myself. I'm not sure if it's possible, maybe someone else will know.

Similar Threads

  1. Replies: 1
    Last Post: 01-11-2008, 11:06 PM
  2. Naming a Class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 12:21 PM
  3. RMI naming problem
    By Robbinz in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 12:32 AM
  4. Naming conventions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:53 AM
  5. error in JNDI naming
    By jitendra.ibs in forum Java Servlet
    Replies: 0
    Last Post: 06-08-2007, 06:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •