• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Instance variables passed in as arguments to static methods?

 
Ranch Hand
Posts: 240
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 240
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it
 
Marshal
Posts: 76881
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic