Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Member Operator Syntax?

 
John Rine
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a heck of a timer understanding certain syntax in Java (and Visual basic for applications) that uses the member access or "dot" operator.
Would some please explain to me exactly what the following statements mean:

whateverVar = myObject.someMethod().someOthermethod(); (Seen in Java)

or

SomethingOrOther.someMethod().someField.SomeotherField (seen in Visual Bsic for applications)

I know this is a Java forum and I do not intend to offend anyone, however, to some degree the use of the member access operator or "dot" operator may be the same in both languages. I am trying to understand what exactly these statements mean.
I tried to write a java application to test the syntax by creating a test class with multiple methods but couldn't get it to work. The only thing I can think of is that the multiple dot operator mean to access a derived class method and then a base class method.
HELP???
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In java (I don't know about visual basic) Objects as well as methods can be members of a class. To access a member of class we can use dot operator on a object of that class.
The dot operator has associativity from left to right. so if a method returns some object then we can use dot operator on method call to access members of returned object.
For example suppose we have 3 classes:



using dot operator we can write:

We can rewrite the above line as:

((objA.objB).getC()).printHi();
this will give: (objB.getC()).printHi();
this will give: (an object of C).printHi();
this will call printHi(); defined in class C
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Rine wrote:
whateverVar = myObject.someMethod().someOthermethod(); (Seen in Java)


we can split this statement for your clarity,
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch John
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would some please explain to me exactly what the following statements mean


It means: someMethod is called on myObject which returns someObject, then someOtherMethod() is called on someObject the result is assigned to whateverVar

Or: first do myObject.someMethod() then do someOtherMethod() on the result of previous call and assign it to whateverVar
 
Nico Van Brandt
Ranch Hand
Posts: 70
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'technique' is called Method chaining
 
John Rine
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Joshi,
Thanks very much, I really appreciate your reply. It is very informative and organized very well. When domeone can explain something the way that you did, it shows that they understand the concept fully.
I asked this question of my C++ and Java instructors but they couldn't explain the concept in terms one could understand-at least me.

thanks again,

John Rine
 
John Rine
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy, Unnar Björnsson, and Nico Van Belle
Thank you also! I really appreciate your replies!
I am very glad I joined.

John Rine
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic