- how to get the current Object reference name?
- Strings and Object References in Java
- Table of contents
- 1. Strings and Object References
- 2. Easy way to Construct Strings
- Easy way to Construct Strings
- Question 2:
- 3. String References as Parameters
- Answer:
- String References as Parameters
- Question 3:
- Strings and Object References in Java
- Table of contents
- 1. Strings and Object References
- 2. Easy way to Construct Strings
- Easy way to Construct Strings
- Question 2:
- 3. String References as Parameters
- Answer:
- String References as Parameters
- Question 3:
how to get the current Object reference name?
posted 12 years ago
I have a question about getting the current reference name, i know i can use getClass().getName() to get the name of the current class.
but how do i get the actuall name of that particular instance?
IS there a way to do that or must i create a method to do it? or is it possible at all?
posted 12 years ago
No, there is no way to do this. The name you give the variable that refers to that object instance is not known to the object.
Consider that you could create multiple variables which ALL had references to the same object; the idea of «the name of» the object instance is meaningless, and this case illustrates that.
You can create a field in an object that contains a name, and assign that name when you create the object, and then retrieve that if you want to.
And now, incidentally, if you create multiple variables that all have a reference to this object, they will all get the same name.
posted 12 years ago
thanks Ralph, I figured each object instance had its own name so thought it could be done.
I suppose i could then use Scanner to create the new objects then they would all have a unique name.
lowercase baba
posted 12 years ago
The reference names are really only useful to the programmer. the JVM (i believe) hashes them out to something that it prefers (I could be wrong on that).
But even if you did get all the names, I don’t see what good it would do you. To create the new name, you’d need a variable to hold THAT, which you’d have to know the name of in advance, unless you wanted to create IT dynamically, in which case.
If you want to create a bunch of new objects, you can use a collection — like an ArrayList, or a Map, or something else. That is much better than trying to come up with a new reference name. Plus there is the fact you can do this:
Boat fredsBoat = new Boat();
Boat staceysBoat = fredsBoat();
The object has two reference variables that point to it — what would be the right name? What happens if we then do this:
String boatOwner = getName(); //Say this returns «fredsBoat
fredsBoat = new Boat(); //Yay. I got a new boat.
paintBoat(boatOwner); //You just painted my old boat.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
posted 12 years ago
I think i understand now, thanks for your help in clearing that up for me.
posted 5 years ago
Is this possible in current vsns of Java?
I would like to be able to reference the name of instances of a class because I’m working on a project where each object is ai different invoice, and I’d like to use the object name as the invoice number.
of course I was thinking invName could be my String variable to pass the object’s name to and use it as the invoice number.
Is there a better way to do this? I did read the other posts on this thread and many on the internet, so that’s where I’m at.
posted 5 years ago
Marshal
posted 5 years ago
- 1
Marshal
posted 5 years ago
posted 5 years ago
Campbell Ritchie wrote:
If an invoice has a serial number, then that serial number would be a field of the Invoice object. That would be the object‑oriented way to do it. This is a naïve implementation:-
.
I can see all sorts of problems with that approach, but don’t think any of those problems is insoluble:-
.
That’s what I was getting at, an articulated solution—if I had time to get serialization working, I’d run with it. And I like your speculation about using a database; in case this was a work project and multiple users would need to keep track of serialized invoice numbers—I will need to figure out something like that in the future. But for now, since this is just an assignment, I lazed out of creating invoice identifiers by asking the user to create and enter a name (which, in my defense, custom invoice naming is standard for programs from Quickbooks to online retailers who allow invoice services—but of course they also keep the last used numbers for reference. ).
Thanks again for showing an approach to serializing. I’m going to tuck this away for the future.
Strings and Object References in Java
The String class is used for text manipulation. As you read, you will learn different ways to create Strings, methods to manipulate Strings, the String concatenation operator ‘+’, and about how Strings are immutable.
Table of contents
- 1. Strings and Object References
- 2. Easy way to Construct Strings
- 3. String References as Parameters
- 4. The null Value
- 5. Null Assigned to any Reference Variable
- 6. The Empty String
- 7. Garbage
- 8. Not Garbage
- 9. Class String
- 10. The concat() Method
- 11. + Operator
- 12. Strings are Forever
- 13. The Length of a String
- 14. The trim() Method
- 15. trim() with Input data
- 16. charAt()
- 17. Column Checker
- 18. Substrings
- 19. Tricky Rules
- 20. Another substring()
- 21. More Tricky Rules
- 22. Control Characters inside String Objects
- 23. Temporary Objects
- 24. The startswith () Method
- 25. More String Operations
- 26. Cascaded String Operations
- 27. End of the Chapter
1. Strings and Object References
In previous chapters, methods were called with parameters that were primitive data types. This chapter discusses how to use object references as parameters. The class String is used in many examples.
Students taking the computer science Advanced Placement examination are expected to be familiar with the String class.
- String literals
- The null value
- More about garbage
- The String class
- String concatenation
- String s are immutable
- Cascading methods
- Some String methods
- concat()
- length()
- trim()
- substring()
- toLowerCase()
- startsWith()
- charAt()
Source: Bradley Kjell, http://programmedlessons.org/Java9/chap44/ch44_01.html
This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 License.2. Easy way to Construct Strings
- A new
String
object is created, containing the characters between quote marks. - A reference to the object is saved in the variable
zeta
.
Easy way to Construct Strings
String objects are very useful and are frequently used. To make life easier for programmers Java has a short-cut way of creating a String object:
String zeta = "The last rose of summer" ;
This creates a String object containing the characters between quote marks. A String created in this short-cut way is called a String literal.
Remember that if several statements in a program ask for literals containing the same characters only one object will actually be constructed (see chapter 41). Other classes do not have short-cuts like this. Objects of most classes are constructed by using the new operator.
Question 2:
Can aString
reference be a parameter for a method?3. String References as Parameters
Answer:
Yes, a
String
reference is often a parameter.String References as Parameters
String stringA = "Random Jottings";
String stringB = "Lyrical Ballads"; if ( stringA.equals( stringB ) )
System.out.println("They are equal.");
else
System.out.println("They are different.");Some methods require a parameter that is a reference to a String object. The example program shows this.
The picture that shows how the method call works. (Both objects have many methods, but only the equals() method of one object is pictured.)
The String object referred to by stringA has an equals() method. That method is called with a parameter, a reference to another String object, stringB . The method checks if both String objects contain identical sequences of characters, and if so, evaluates to true.
Careful: The previous paragraph is correctly stated, but awkward. People often say "String" when they really mean "reference to a String". This is fine, but remember that a reference variable like stringA does not contain an object, but only a reference to an object. This may seem picky, but there is nothing quite as picky as a computer. Students who are not careful about this often run into problems.
Question 3:
(Review:) Examine the following snippet of code.
Answer the questions using careful words (like the above on the right).String a; Point b;
- What is the data type of the variable a?
- What is the data type of the variable b ?
- How many objects are there (so far)?
- 1. Strings and Object References
- 2. Easy way to Construct Strings
- 3. String References as Parameters
- 4. The null Value
- 5. Null Assigned to any Reference Variable
- 6. The Empty String
- 7. Garbage
- 8. Not Garbage
- 9. Class String
- 10. The concat() Method
- 11. + Operator
- 12. Strings are Forever
- 13. The Length of a String
- 14. The trim() Method
- 15. trim() with Input data
- 16. charAt()
- 17. Column Checker
- 18. Substrings
- 19. Tricky Rules
- 20. Another substring()
- 21. More Tricky Rules
- 22. Control Characters inside String Objects
- 23. Temporary Objects
- 24. The startswith () Method
- 25. More String Operations
- 26. Cascaded String Operations
- 27. End of the Chapter
- String literals
- The null value
- More about garbage
- The String class
- String concatenation
- String s are immutable
- Cascading methods
- Some String methods
- concat()
- length()
- trim()
- substring()
- toLowerCase()
- startsWith()
- charAt()
Source: Bradley Kjell, http://programmedlessons.org/Java9/chap44/ch44_01.html
This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 License.2. Easy way to Construct Strings
- A new
String
object is created, containing the characters between quote marks. - A reference to the object is saved in the variable
zeta
.
Easy way to Construct Strings
String objects are very useful and are frequently used. To make life easier for programmers Java has a short-cut way of creating a String object:
String zeta = "The last rose of summer" ;
This creates a String object containing the characters between quote marks. A String created in this short-cut way is called a String literal.
Remember that if several statements in a program ask for literals containing the same characters only one object will actually be constructed (see chapter 41). Other classes do not have short-cuts like this. Objects of most classes are constructed by using the new operator.
Question 2:
Can aString
reference be a parameter for a method?3. String References as Parameters
Answer:
Yes, a
String
reference is often a parameter.String References as Parameters
String stringA = "Random Jottings";
String stringB = "Lyrical Ballads"; if ( stringA.equals( stringB ) )
System.out.println("They are equal.");
else
System.out.println("They are different.");Some methods require a parameter that is a reference to a String object. The example program shows this.
The picture that shows how the method call works. (Both objects have many methods, but only the equals() method of one object is pictured.)
The String object referred to by stringA has an equals() method. That method is called with a parameter, a reference to another String object, stringB . The method checks if both String objects contain identical sequences of characters, and if so, evaluates to true.
Careful: The previous paragraph is correctly stated, but awkward. People often say "String" when they really mean "reference to a String". This is fine, but remember that a reference variable like stringA does not contain an object, but only a reference to an object. This may seem picky, but there is nothing quite as picky as a computer. Students who are not careful about this often run into problems.
Question 3:
(Review:) Examine the following snippet of code.
Answer the questions using careful words (like the above on the right).String a; Point b;
- What is the data type of the variable a?
- What is the data type of the variable b ?
- How many objects are there (so far)?
Strings and Object References in Java
The String class is used for text manipulation. As you read, you will learn different ways to create Strings, methods to manipulate Strings, the String concatenation operator '+', and about how Strings are immutable.
Table of contents
1. Strings and Object References
In previous chapters, methods were called with parameters that were primitive data types. This chapter discusses how to use object references as parameters. The class String is used in many examples.
Students taking the computer science Advanced Placement examination are expected to be familiar with the String class.