File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A quote from Khalid A. Mughal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A quote from Khalid A. Mughal" Watch "A quote from Khalid A. Mughal" New topic

A quote from Khalid A. Mughal

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Quote from page 121:

Static methods are also known as class methods. A static method in a class can directly access other static members in the class. It cannot access instance members of the class, as there is no object being operated on when a static method is invoked. Note, however, that a static method in a class can always use a reference of the class's type to access its member, regargless of the whether these members are static or not.

How can static method access non static members?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Barkat, if the static method is given (passed), or creates, an instance of it's class it can access the members of the class. That's how I interpret it anyway.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Hi Barkat,
Itz how we access the static variables in any main method.
the following is an example. I think Khalid Mughal's book wants to say the same..

[ October 14, 2002: Message edited by: shweta mathur ]

--Shweta<br />SCJP 1.4 <br />SCWCD
Sanjay Saxena
Ranch Hand

Joined: Dec 12, 2001
Posts: 81
I have 2000 publication of Khalid'd book. I'm starting to study for 1.4, please advise me if there is any new publication from KM for 1.4
Do the topics covered in this book except AWT and Swing are enough or do i have to refer more books
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Well if you have a static method, it can access static members (variables and methods) this we agree on, but what if it wants to access non-static members (instance members)?
First instance members don't exist until an instance (object) of this class is created right?
So for a static member to access an instance member, an instance of the class itself has to be created.

Got the picture?
[ October 14, 2002: Message edited by: Alfred Kemety ]

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
I agree. Here's the link:
subject: A quote from Khalid A. Mughal
It's not a secret anymore!