Results 1 to 4 of 4
- 01-19-2013, 01:18 AM #1
Member
- Join Date
- Jan 2013
- Location
- Texas
- Posts
- 41
- Rep Power
- 0
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
- 01-19-2013, 01:23 AM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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.
- 01-19-2013, 06:18 AM #3
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).
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-19-2013, 04:19 PM #4
Member
- Join Date
- Jan 2013
- Location
- Texas
- Posts
- 41
- Rep Power
- 0
Similar Threads
-
XSLT | disable-output-escaping adding a new line to the output
By smarty_m2002 in forum Advanced JavaReplies: 2Last Post: 05-03-2012, 11:39 AM -
Need to write ResultSet string output of one class to the JTextArea of a GUI class
By cknapick in forum New To JavaReplies: 3Last Post: 04-27-2012, 02:41 PM -
Creating an instance variable in one class that connects to another instance variable
By SpicyElectricity in forum New To JavaReplies: 1Last Post: 04-21-2012, 06:03 PM -
How create new instance of a class given the classname in string form?
By Addez in forum New To JavaReplies: 2Last Post: 08-15-2011, 08:05 AM -
Input output without string class
By surenq1 in forum New To JavaReplies: 4Last Post: 06-13-2010, 03:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks