Results 1 to 4 of 4
  1. #1
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Question Adding the Class instance name to an output string

    Greetings all,
    I am working on a college project I will tell you that up front. I have completed the project to create a car class and to have it accelerate and brake. My question does not involve any part of the actual assignment requirement, but I wanted to add a method that would allow me to print the attributes and when it prints I wanted it to add the name of the instance to the print statement

    for example: I have a class called CarClass and I create new instances as follows:
    CarClass newName = new CarClass("color","make","model","year","owner");

    I have created a couple of classes as such:

    CarClass firstCar = new CarClass("Red","Chevy","Malibu","2005","Bob");
    CarClass secondCar = new CarClass("Blue","Saturn","Aura","2007","Sandy");

    I have a printAttrbutes method in the CarClass that will do the following:

    public void printAttributes()
    {
    System.out.printf("%s owns a %s %s %s, myOwner, myYear, myMake, myModel);
    }

    all of that works fine but I am trying to make the print statement include the name of the CarClass instance for example:
    if I were to print the attributes about the secondCar I would like to print: secondCar is owned by Sandy and is a 2007, Saturn Aura
    I am not sure how to get it to add the instance name to the print string.

    I hope what I am asking is clear enough, if not please reply or shoot me an email and I will respond.

    thanks
    Wally

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Adding the Class instance name to an output string

    As far as I know, the name you give the object is not now to the object. The only way to do this is to hardcode it in.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Adding the Class instance name to an output string

    A reference variable (aka identifier) is in no way an 'instance name' and doesn't exist at runtime.

    Objects don't have names (unless of course the class has a name attribute, but that's another story).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Adding the Class instance name to an output string

    Interesting. I have never thought of it that way. Thanks for the feedback. Gives me food for thought. I was just trying to make it more intuitive when the user was looking at the output. The user being the teacher :)

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2012, 11:39 AM
  2. Replies: 3
    Last Post: 04-27-2012, 02:41 PM
  3. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  4. Replies: 2
    Last Post: 08-15-2011, 08:05 AM
  5. Input output without string class
    By surenq1 in forum New To Java
    Replies: 4
    Last Post: 06-13-2010, 03:18 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
  •