This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 31
3
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 38571
661
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Posts: 38571
661
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 38571
661
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!