p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Java > Java and JDK > BOOK: Beginning Java 2
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 8th, 2009, 04:09 AM
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 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old June 8th, 2009, 05:00 PM
Friend of Wrox
Points: 4,805, Level: 29
Points: 4,805, Level: 29 Points: 4,805, Level: 29 Points: 4,805, Level: 29
Activity: 52%
Activity: 52% Activity: 52% Activity: 52%
 
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*.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old June 9th, 2009, 04:59 AM
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old June 9th, 2009, 08:31 PM
Friend of Wrox
Points: 4,805, Level: 29
Points: 4,805, Level: 29 Points: 4,805, Level: 29 Points: 4,805, Level: 29
Activity: 52%
Activity: 52% Activity: 52% Activity: 52%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 02: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 06:08 PM
Static methods spinjob BOOK: Professional C++ 7 March 12th, 2009 03:16 PM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 02:48 PM
METHODS magagulad Java GUI 3 May 15th, 2007 01:53 PM



All times are GMT -4. The time now is 02:40 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc