Java: Static vs Instance Methods — a definition
Static methods in Java are also called "class methods" as they are shared by all instances of a class (aka objects of a class). So static methods can be used to increment a class field that is also shared by all instances of a certain class. A class field is just a variable within the class scope. This variable might be used to increment a serial number which would be independently accessible by all instances of a certain class.
Instance methods are just regular object methods that can be only called by an instantiated object and which are only relevant within a specific class instance (aka object).
class MyClass {
private static int myClassId = 1; // this is a class field,
// this variable is called "field" because it has a class-wide scope
private int myObjectId = 1; // this is an instance field,
// this variable is called "field" because it has a class-wide scope
public static void incMyClassIdIncrementer() { // this is a class method
myClassId++; // this is bad style as this field could be confused with an instance field
MyClass.myClassId++; // this statement is equivalent to the above statement,
// static fields should be referred to using class' name
System.out.println("myClassId: " + MyClass.myClassId);
}
public void printMyObjectId() { // this is an instance method
// the this keyword is optional and refers to "within this class"
System.out.println("myObjectId: " + this.myObjectId);
}
}
MyClass.incMyClassIdIncrementer(); // this is is the only way to call class methods
MyClass myClass = new MyClass(); // here a class is instantiated and its instance,
// aka the object is assigned to a variable
myClass.printMyObjectId(); // this is the only way to call instance methods,
// by referring to them via object variables
Instance methods are just regular object methods that can be only called by an instantiated object and which are only relevant within a specific class instance (aka object).
class MyClass {
private static int myClassId = 1; // this is a class field,
// this variable is called "field" because it has a class-wide scope
private int myObjectId = 1; // this is an instance field,
// this variable is called "field" because it has a class-wide scope
public static void incMyClassIdIncrementer() { // this is a class method
myClassId++; // this is bad style as this field could be confused with an instance field
MyClass.myClassId++; // this statement is equivalent to the above statement,
// static fields should be referred to using class' name
System.out.println("myClassId: " + MyClass.myClassId);
}
public void printMyObjectId() { // this is an instance method
// the this keyword is optional and refers to "within this class"
System.out.println("myObjectId: " + this.myObjectId);
}
}
MyClass.incMyClassIdIncrementer(); // this is is the only way to call class methods
MyClass myClass = new MyClass(); // here a class is instantiated and its instance,
// aka the object is assigned to a variable
myClass.printMyObjectId(); // this is the only way to call instance methods,
// by referring to them via object variables
you cannot use the "this" keyword inside a static method.
ReplyDeleteYou are right. I've fixed it. Thanks for reporting it!
ReplyDelete