Thread: Static Methods
View Single Post
  #2 (permalink)  
Old June 8th, 2009, 05:00 PM
Old Pedant Old Pedant is offline
Friend of Wrox
Points: 4,805, Level: 29
Points: 4,805, Level: 29 Points: 4,805, Level: 29 Points: 4,805, Level: 29
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 Posts
Default

No no no...you have misinterpreted the meaning of that note.

In your code, you are *NOT* accessing an instance variable of the *CLASS* from within that static method. You are accessing an instance variable of the *OBJECT* that was passed in to the function!!!

*THINK* about it: It wouldn't make any difference WHAT object was passed to the static function. If the member you are referencing is valid, the function could get it.

Example:
Code:
public class Practice {
       
    public static int getAge(Zamboni zamb)
    {
        return zamb.age;
    }
}
What you code is NO DIFFERENT than my example!

It's also POINTLESS code.

There's no reason to pass an instance of the SAME CLASS into a static method of that class.

If you are going to do something like that, WHY is the method static in the first place???

A better way to see the error that the reference is talking about would have been to have coded something like this:
Code:
package source.hibernate.utilities;

    public class Practice {
    public String name;
    public int age;
           public static int getAge( ) { return age; }
       }
Try *THAT*.
Reply With Quote