Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

A static member accessing a non-static member of another class (Sybex Mock Exam 1)

 
Ranch Hand
Posts: 31
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!

I find hard to understand the following code taken from Sybex's first Mock Exam:





The code compiles and prints 30. Unfortunately, I thought that it wasn't going to compile due to the fact that the go() method is static whereas Car's member 'velocity' isn't.

How is that possible? Does the whole “non-static can access static and non-static but static can only access static” limits to members of the same class?

In any case, the exam's question is (at least for me) really tricky! I'm glad I found it in a mock exam instead of the real one.

Thanks!

Regards,

Tomás
 
author & internet detective
Posts: 41578
881
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code has  c.velocity += 10; This means you are accessing the instance variable velocity on the Car object referenced by c. That's fine.

If it just said velocity += 10; you would have the problem you mentioned about accessing a non-static variables.

You could move the go() method to another class and it would still compile for that matter. The fact that it is in the Car class at all is a happy coincidence. Or an evil trick .
 
Tomas Castagnino
Ranch Hand
Posts: 31
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I get it! I suppose that's the same reason why we need to instantiate a class in order for the non-static methods' call from inside main() to compile in some classes.

Thanks a lot!
 
Tomas Castagnino
Ranch Hand
Posts: 31
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, Jeanne, I think I there is an erratum in the errata: in the errata list the typo in page 111 (AnimAl) appears fixed when it actually isn't (I believe I have the second printing of the book and it's still "Animal").
 
Jeanne Boyarsky
author & internet detective
Posts: 41578
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tomas Castagnino wrote:I get it! I suppose that's the same reason why we need to instantiate a class in order for the non-static methods' call from inside main() to compile in some classes.


Correct!


Tomas Castagnino wrote:By the way, Jeanne, I think I there is an erratum in the errata: in the errata list the typo in page 111 (AnimAl) appears fixed when it actually isn't (I believe I have the second printing of the book and it's still "Animal").


I have a copy of the second printing and it looks right to me.
 
Tomas Castagnino
Ranch Hand
Posts: 31
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

I have a copy of the second printing and it looks right to me.



I'm referring to the “Animal” in the paragraph that begins with “This code is equivalent (…) and outputs Animal”.
 
Jeanne Boyarsky
author & internet detective
Posts: 41578
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomas,
Oh! That wasn't an error in the erratum. It was me not knowing that typo existed in two places in the first place. Which meant we only reported one to Wiley and they only fixed one. I added this as a new errata. Thanks!
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic