This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes class variables exp w/? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class variables exp w/?" Watch "class variables exp w/?" New topic
Author

class variables exp w/?

Jennifer Schwartz
Ranch Hand

Joined: Jan 12, 2011
Posts: 46



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

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Jan 12, 2011
Posts: 46

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

Joined: Jan 12, 2011
Posts: 46

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

Joined: Oct 02, 2003
Posts: 11168
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jennifer Schwartz
Ranch Hand

Joined: Jan 12, 2011
Posts: 46

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

Joined: Oct 13, 2005
Posts: 38045
    
  22
Careful with thread titles; what does "exp w/?" mean?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jennifer Schwartz
Ranch Hand

Joined: Jan 12, 2011
Posts: 46

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

Joined: Oct 13, 2005
Posts: 38045
    
  22
Jennifer Schwartz wrote: . . . I was dreaming about code last night.. . . .
There are far worse things to dream about
 
jQuery in Action, 2nd edition
 
subject: class variables exp w/?
 
Similar Threads
How to add in button and paint in applet?
Thread question
Exeptions with Threads
Synchronized methods
why thread priority is not working here