Thread: Static Methods
View Single Post
  #3 (permalink)  
Old June 9th, 2009, 04:59 AM
tejasbhojak tejasbhojak is offline
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx a lot for ur reply :),
i guess, i have not clearly explained my doubt.
I understand that there is no sense to pass Instance of same class to the static method,instead we could write one non-static method .

From what i have read about static methods(which may be insufficient) is , static methods cannot access not static member variables.
So Ideal way of writing a static method should be

static anyStaticMethod(Arguments that does not include References)
{
//code , using all static variables of class, or any primitive values passed
// as arguments
// finally return some thing(if required)
}

(please correct me if am wrong some where)

but if u need to write a method where u have to use references to objects
and modify them which is a normal scenario , then u can do it using a non static method.
i believe this may improve the efficiency of code.
So at compile time itself we should get an error , if there is an non static reference to a static method.
but we don't get such error while we are compiling above code.


and ya, in this code
Code:
package source.hibernate.utilities;

    public class Practice {
    public String name;
    public int age;
           public static int getAge( ) { return age; }
       }
there would be an error , because age is associated to object of class practice
and it would change with every other object that u create, so u need to specify the reference hence return prac.age would work.
Reply With Quote