Wrox Programmer Forums
|
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2009, 03:09 AM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Static Methods

I have one doubt on Static methods
Most common line that i often read about Static methods is

"Remember that you cannot directly refer to any of the instance variables in the class
within a static method. This is because a static method can be executed when no
objects of the class have been created, and therefore no instance variables exist."

i tried accessing instance variable in static method, i thought compiler would not allow that , but i could easily
compile and run the code
Code:
package source.hibernate.utilities;

    public class Practice {
       
        public String name;
        public int age;
       
        public static int getAge(Practice prac)
        {
            return prac.age;
        }
       
    }
/*------------------------------------------------*/
package source.hibernate.utilities;

public class Test
{
   
    public static void main(String argc[])
    {
        Practice prac =new Practice();
       
        prac.age=10;
        System.out.println(Practice.getAge(prac));
       
    }
}
Now in above code both the classes Practice and Test are in same package
i tried printing age which is a non-static member variable of class Practice through
a static method getAge().
i could compile and execute the above code with out any error.

Now this is contradicting my understanding for static methods.
static methods cannot access any instance variable,
but if i think it as a normal non-static method, where i provide it with an reference to an instance and then print the age
using that reference then its perfect!

shouldn't compiler be checking any non static reference made to a static methods at compile time.
And why java allows a static method to be called from object.
I guess my understanding for static methods is still not perfect.
 
Old June 8th, 2009, 04:00 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 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*.
 
Old June 9th, 2009, 03:59 AM
Registered User
 
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.
 
Old June 9th, 2009, 07:31 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You seem to have just said the same thing I said, in different words.

So I really do not understand your "doubt."

The point is, when you code
Code:
       public static int getAge(Practice prac)
       {
            return prac.age;
       }
the member age that you are getting there is *NOT* the CLASS variable named age. It is the OBJECT member that is a member of the REFERENCE "prac" that you passed to the function.

There is nothing illegal at all about getting a member of a referenced instance, no matter where. The fact that your getAge method is static is IRRELEVANT.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do we need to maintain "static" methods in the Article.cs? Jyothi_23 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 April 13th, 2009 01:55 PM
Using Static variables in Static Class JoinTTT BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 4 March 29th, 2009 05:08 PM
Static methods spinjob BOOK: Professional C++ 7 March 12th, 2009 02:16 PM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
METHODS magagulad Java GUI 3 May 15th, 2007 12:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.