aspose file tools*
The moose likes Beginning Java and the fly likes Member Operator Syntax? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Member Operator Syntax?" Watch "Member Operator Syntax?" New topic
Author

Member Operator Syntax?

John Rine
Greenhorn

Joined: Jun 19, 2011
Posts: 3
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

Joined: Jun 10, 2011
Posts: 207

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

Piyush
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

John Rine wrote:
whateverVar = myObject.someMethod().someOthermethod(); (Seen in Java)


we can split this statement for your clarity,
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

And welcome to JavaRanch John
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
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

Joined: Mar 31, 2011
Posts: 66

The 'technique' is called Method chaining


Oracle Java SE6 Certified Programmer
Oracle Java EE5 Certified Web Component Developer
John Rine
Greenhorn

Joined: Jun 19, 2011
Posts: 3
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

Joined: Jun 19, 2011
Posts: 3
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Member Operator Syntax?