• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sybex OCA Java 8 Study Guide, p.216

 
Mike J. Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. This is my first post here at javaranch so I just want to start by saying a big THANK YOU to all the posters and answerers who make this forum great! Javaranch has been far-and-away the most helpful resource I've used in planning my OCA attempt.

I'm almost finished with Jeanne and Scott's Study Guide. My first question is:
On p. 216, the last sentence on the page is "Identify when to call static rather than instance methods based on whether the classname or object comes before the method" which seems to insinuate that when an objectRef "comes before the method" Java can't call a static method.
But doesn't that contradict the last couple paragraphs of p.182 where it states that you actually CAN call a static member with the syntax objectRef.member rather than className.member ?

... I just want to make sure I'm not misunderstanding how to properly interpret that syntax.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
Welcome to CodeRanch!

One of those is about what is common/good practice. The other is about what is allowed.

Your question shows you do in fact understand there is a distinction, which is good.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike J. Jones,

First of all, a warm welcome to CodeRanch!

Mike J. Jones wrote:This is my first post here at javaranch so I just want to start by saying a big THANK YOU to all the posters and answerers who make this forum great! Javaranch has been far-and-away the most helpful resource I've used in planning my OCA attempt.

Very glad to hear you are having a blast here on CodeRanch

Mike J. Jones wrote:On p. 216, the last sentence on the page is "Identify when to call static rather than instance methods based on whether the classname or object comes before the method" which seems to insinuate that when an objectRef "comes before the method" Java can't call a static method.
But doesn't that contradict the last couple paragraphs of p.182 where it states that you actually CAN call a static member with the syntax objectRef.member rather than className.member ?

I have to agree with you! Based on the last sentence on page 216, you can determine which method should be invoked based on whether the class name or an object comes before the method. But that's incorrect! Akthough it's considered to be a bad practice, it's allowed to invoke a class (static) method using an object (reference variable) instead of the class name, illustrated in this code snippetIf you run this code snippet, you get the following output:
I am a static method!
I am an instance method!
I am a static method!
I am a static method!

Even when a reference variable is null, you can use it to invoke a class (static) method (and no NullPointerException is thrown at runtime). So the last sentence on page 216 should probably be rephrased a little bit to be 100% accurate. But for the exam it's very obvious you should be able to determine which methods are executed in a code snippet to correctly determine the output. And with overloading and overriding that might be really tricky

Hope it helps!
Kind regards,
Roel
 
Mike J. Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel! It's good to know I'm on the right track.
It's great how that code snippet you supplied shows every possible syntax: legal and illegal. That makes it very clear!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike J. Jones wrote:It's great how that code snippet you supplied shows every possible syntax: legal and illegal. That makes it very clear!

Very glad to hear you like it! That was exactly what I was aiming for with that post
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic