File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class variables exp w/?

 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Print results should be:
18
18
18
18

QUESTION: I think I have grasped the concept that the value of y is being shared among all objects, but how is it determined to return 18 specifically?? AND..where does the ivar of x come into play??
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jennifer Schwartz wrote:[...] but how is it determined to return 18 specifically??

Because it's the last value assigned to it.
Jennifer Schwartz wrote: AND..where does the ivar of x come into play??

It doesn't.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that was simple enough! I haven't come across a situation where multiple values were instantiated this way in my studies. I'll just remember to look @ the last value if I see it again. After looking over my notes, I did have another example of ivars (using the same code) and so the author just left it in the example for the class var.

And just so I'm clear about access, a static variable/method cannot access a non static and vice versa?


Thanks so much!
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, forgot about an object ref being used from a static method to access instance vars. Is that the only exception?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a static cannot access a non-static, but it is not true the other way around. a static variable exists whether an instance is created or not. it is always there, so you can reference it from a static or non-static methods.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm assuming you mean it's the the other say around from my reply. Thanks for the assist but my head is going in circles at this point and I'll just have to come back to this later.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful with thread titles; what does "exp w/?" mean?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand what static means exactly with regard to member variables?

It means that there is only one copy of the variable that is shared by all instances of the class, rather than that there is a separate copy of the variable for each instance (which is the case for non-static member variables). So:

y is a static member variable, which is shared by all Thing objects. If you change the value of Thing.y, as you are doing in lines 22, 23 and 24, then you are changing the one variable that exists that is shared by all Thing objects. It doesn't matter on which instance of Thing you do this (you're doing it for three different Thing instances) - you're still modifing the one shared static variable.

Lines 26 to 29 are just printing the exact same variable four times, so you see the same value four times.

Note that it is bad style to refer to static member variables or static methods via instances of the class; you should really access them using the classname, not using a specific instance of the class. For example:

Accessing static members through an instance is bad style, because it hides the fact that you're accessing a member that is shared between all instances of the class - it looks like you're accessing a variable of just that instance, which is confusing. (In my opinion, it should not even have been allowed in the Java language that this is possible - I don't see any use case where you'd ever need to access a static member through an instance).

For more details, see: Understanding Instance and Class Members in Oracle's Java Tutorials.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeng! I do understand the sharing distinction between the two. You do have a great way of explaining it though and I wish I had seen it a few days ago! Earlier today, I did not understand why the last variable was the answer. But understanding what I do now, it made sense because it was a static and not an instance variable.

As for the sloppy code...I can't take responsibility for that directly. I copied it from a worksheet from the Stanford Java videos ;) And here Professor Sehami is always talking about writing presentable code! hmph

I have to take the Java Programmer cert by next month and I know I shouldn't be cramming, but I don't have a choice. So, I will probably be posting quite often ;) I was dreaming about code last night...wonder what that means??? haha.

THANKS again!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jennifer Schwartz wrote: . . . I was dreaming about code last night.. . . .
There are far worse things to dream about
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic