• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

bending the static rule??

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok the thing is like ....this according to what i know.. a static code block cannot access instance variables.. but wat if instance variables are passed as parameters to a static member function?? will the rule bend?? will a static block be then able to access instance variables?? if yes then can anyone explain why this happens...
cheers
nik
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean something like this?



You cannot access instance variables from a static context, simply because to the static method the instance variables do not exist. You can pass the value of an instance variable to a static method but there would be no point. You would be better off defining a non-static method instead.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, a static method can access the instance variables of a parameter (of the same type) that is passed in, or that it has obtained in another way (for example by creating it)

Originally posted by Stian Almaas:
You can pass the value of an instance variable to a static method but there would be no point. You would be better off defining a non-static method instead.



There are uses. Some that spring to mind are:
  • Factory methods (setting instance variables after the constructor)
  • Utility methods (e.g. extract common information from a parameter list)
  • Manipulating a static Singleton


  • In general, however, it's probably better to go with waht Stian said, and put the behaviour directly on the instance.
     
    Ranch Hand
    Posts: 531
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    > what if instance variables are passed as parameters to a static member function?

    What if the static value was loaded from a file, uplinked to a satellite, downloaded by a .NET application server and sent through a SOAP message to a process that called that static method? Tongue-in-cheek of course but the point is you do not have a reference to a static value; you have a parameter whose argument may have any number of additional references.

    Here's a less elaborate example. You cannot access private instance variables from outside the declaring class. Is it bending the rule to return a reference to it?

    private String secret;

    public String getSecret() { return secret; } // Bending rule? No.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic