This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static question again !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static question again !!" Watch "Static question again !!" New topic
Author

Static question again !!

Shaan patil
Ranch Hand

Joined: Dec 22, 2007
Posts: 58
Hi all,

If main is a static method , Then how is that IT IS POSSIBLE TO acccess non static members(variables & methods) from main method using dot operator on object reference ?

Regards
Shaan Patil

"You canot change the truth but truth changes you "


"You cannot change the truth but truth can change you "
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Howdy Shaan.patil,

Welcome to JavaRanch

You need to please adjust your display name according to the Ranch's policy. As such you can just remove the "dot" in between your names. That would suffice
[ January 01, 2008: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Shaan.patil:
..If main is a static method , Then how is that IT IS POSSIBLE TO acccess non static members(variables & methods) from main method using dot operator on object reference ?


You have an answer in your question itself. You are accessing the non-static members using the dot operator on an object reference!

Try accessing any of the non-static members inside main without using an object reference. You will get a compiler error saying that non-static variable <varName> cannot be referenced from a static context.
[ January 01, 2008: Message edited by: Raghavan Muthu ]
Shaan patil
Ranch Hand

Joined: Dec 22, 2007
Posts: 58
Hi Raghavan Muthu,


Methods declared as static have several restrictions:
� They can only call other static methods.( This is my doubt )
� They must only access static data.
� They cannot refer to this or super in any way.

If static methods can call only static methods then how come we can access non static members of other class using object reference ???
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Shaan.patil:


Methods declared as static have several restrictions:
� They can only call other static methods.( This is my doubt )
� They must only access static data.
� They cannot refer to this or super in any way.



All the aforesaid points are absolutely correct Shaan.


..If static methods can call only static methods then how come we can access non static members of other class using object reference ???


But this holds good when you tend to directly access the non-static members of the class. Means, without any reference, you can directly call only the static members of the class.

You can very well pass an object to a static method and using that object reference you can access the non-static members (method or data) inside the called method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18486
    
  40

"Shaan.patil",

[EDIT: Warning deleted. Thanks for fixing the screen name.]


As for your question...

If static methods can call only static methods then how come we can access non static members of other class using object reference ??


Static methods doesn't have any reference to an instance, hence, it can't call any instance methods directly. Using an object reference to call an instance method solves this issue. As for your list ....


They can only call other static methods.( This is my doubt )


Again, for instance methods -- not directly. And the reason is because there isn't an instance.

They must only access static data


Again, for instance methods -- not directly. And the reason is because there isn't an instance.

They cannot refer to this or super in any way


The "this" and "super" are just special references to the current instance. And again, there isn't an instance.

Thanks,
Henry
[ January 01, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Shaan,

Perhaps this code would help you i guess.



Running the above code produces the following output:



I think the code is self explanatory. Just look at the static method "printMyValue()".

Also when you uncomment the Line 2, you will get a compiler error. And that's what you had been asking for and i had been telling you. Since it is a non-static method.

When you try to directly access any non-static member of a class inside a static method, you will get a compiler error. It is because the static method does NOT know with respect to which specific instance it should invoke the instance (or non-static) member.

But you can very well access a non-static member inside a static method using an object reference of the class. That does NOT cause any harm as you are sure that you have a finite object in your hand [Example, our printMyValue() method).

Note: Look at the way the printMyValue() method has been called. Since it is static it is called directly. No matter from where you call a static method as static methods can be invoked from non-static methods also. Had it been a non-static, instance level method you will get a compiler error as per the reasons i have explained earlier.

Does that clear your doubt now?
[ January 01, 2008: Message edited by: Raghavan Muthu ]
Shaan patil
Ranch Hand

Joined: Dec 22, 2007
Posts: 58
Thanks a lot Raghavan and Wong ,

Now you have hit the bulls eye,It has solved my doubt clearly.

So in Conclusion we can reference any public method (static or non static)using object reference with dot operator in any method static or otherwise, right ?

Thanks a lot for the illustrative code.

Regards
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Yes absolutely.

But NOT only public methods. Just any instance method with any access level as far as a single class is concerned.

Obviously when inheritance comes into picture, you got to think about the method you gonna call and its visibility outside.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static question again !!
 
Similar Threads
Static method related doubts
A proper "main()" signature
Load on startup
Can anyone explain this please ??
Static Methods