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 statin vs. nonstatic methods 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 "statin vs. nonstatic methods" Watch "statin vs. nonstatic methods" New topic

statin vs. nonstatic methods


Joined: Jul 08, 2000
Posts: 20
hi friends,
this one is from a site (of 3 peoples, one was raji or so , I don't remember the exact names)
"Any non-static method will throw a NullPointerException if the calling object reference is null, static methods are ok."
I can't understand the meaning of the statement.
anyone pls help.
thanks in advance,
[This message has been edited by Ravindra (edited August 20, 2000).]
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I am by no means an expert, but I believe what they are saying is to call a non-static method of an object, the object must live somewhere (i.e. someone called new xxx() ). Static methods do not require this because they only operate on "static" data which is created when a class is loaded.
Here's a simple example:
public class MyClass {

static int a = 0;
int b = 1;

static void int getA() {
return a;

void int getB() {
return b;
public static void main(String args[]) {
int myInt = 0; //must initialize variables inside methods
myInt = MyClass.getA(); // can call this because it's static
MyClass mc; // mc is initialized to null (default of objects)
myInt = mc.getB(); // this is an error: no instance of MyClass loaded yet
mc = new MyClass(); //normally you would call this at declaration

myInt = mc.getB(); // now it's okay

Hope that helps.
I agree. Here's the link:
subject: statin vs. nonstatic methods
It's not a secret anymore!