Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance variables passed in as arguments to static methods?  RSS feed

 
T Vergilio
Ranch Hand
Posts: 231
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in a muddle again

I thought static methods could never use instance variables, because they wouldn't know which instance to look at.

From Head First Java, p. 284: "A static method is not associated with a particular instance - only the class - so it cannot access any instance variable values of its class. It wouldn't know which instance's values to use."

Now I was answering some mock exam questions from Cameron McKenzie's SCJA book, and I don't understand one of the options. On page 205, the last question has an option that says: "Instance variables ... are not visible in static methods, unless passed in as arguments." This option is supposed to be correct.

Now... how does that work?
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That option would be incorrect.

When you pass an instance variable to a static method (or any other method for that matter) you pass the value of that variable. Not the variable itself. That's probably what the author meant.
The variable itself however, doesn't become available.
 
T Vergilio
Ranch Hand
Posts: 231
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers, mate. You mean the value of an instance variable can be passed to a static method as an argument, if you specify the object, i.e. objectName.variable?

This would pass the value of objectName.variable to the static method?
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you pass an instance field to a static method, it is not a case of the static method seeing an instance member. It is more a case of an instance method (whichever passed the instance field) seeing the static method to pass it the information. Both quotes are correct, but each includes some information missed out from the other.
 
yan ming
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So , which is correct ? A or B

A. Instance variables will not be visible in Static methods if it passed as a parameter.
B. Instance variables will be visible in Static methods if it passed as a parameter.
 
Dave Tolls
Rancher
Posts: 2913
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yan ming wrote:So , which is correct ? A or B

A. Instance variables will not be visible in Static methods if it passed as a parameter.
B. Instance variables will be visible in Static methods if it passed as a parameter.


The instance variable is still not visible.
What is visible is the methods parameter, which happens to hold the same value as the instance variable, ie the same primitive value or reference the same object on the heap.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!