• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

How does equals() of a String object access the private value of a second string object ?  RSS feed

 
Ranch Hand
Posts: 64
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I get slightly obsessed with little details which are often  beyond the scope of my immediate study purposes but I can't move on until they're resolved. I don't know whether this is a good or bad thing.

Anyway,

Suppose str1 refers to one String object and str2 refers to another, and I want to compare these for equaility.

The equals() method of str1 object essentially compares its own private final value with that that of the second string. How does it again access to the other sting object's private final value ?

Thanks for any responses.

Mohammed.
 
author & internet detective
Posts: 39244
727
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed,
Good question! And curiosity is definitely a good thing!

"private" means that you can only access values of the same class. However, it still allows different instances of the same class to access each other's private variables. Think of it as "private to the class" vs "private to the object".
 
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accessibility of private members does not work the way you think. Access is determined at compile time and there are no object instances at compile time so the compiler has to use code structure and scope rules. The JLS chapter 6 section 6 has the information about accessibility of members but it's not easy to understand the JLS.
 
Mohammed Azeem
Ranch Hand
Posts: 64
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah..ha.  Excellent, solved.
Thank you both.

Mohammed.
 
BWA HA HA HA HA HA HA! Tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!