ddStatic vs Non-static Methods:
What is a Non-Static Method?
• A non-static method is a method that we say “belongs to
the object”
• When you instantiate (create) a new object, a chunk of RAM
gets reserved and copies of all the parent methods (and
fields) are written into that space.
• Each object has inherited its own set of methods (this is one
of the foundations of OOP)
• The Gogga object has its own copy of move(), setDirection(),
etc
• ALL of these inherited methods are non-static methods
• You call a non-static method by using the object name
(Gogga bob = new Gogga();
bob.setPosition(5,7);)
What is a Static Method?
• A method that is “global” it is common to all objects.
• Not every object can inherit its own copy of such a method.
• Single instance of this method is held instead by the class
• Static method “belongs” to the class
• ALL the Gogga object that we create sit on the same grid
• setGridSize() method is static because ALL Goggas use the
same Grid
• You call static methods by calling their class name and also
the String class (char upperChar =
Character.to.UperCase(‘a’);)
How Does this Idea Extend to Fields?
• Fields (and even classes) can also be static or non-static
• A class called Dog(), that will create new instances of dog
objects each time it is invoked(run/executed)
, • numberOfLegs field is ALWAYS going to be 4. So, there is
no need for every dogs to have it’s own copy of this field
• It is better for Class Dog() to have single, static field, that
the individual objects would then just reference if needed
• But hairColour would be non-static field, because dogs have
a wide variety of different colours. So, each object would
need its own copy of this field
• Non-static field, or fields that belong to an instance of a
class (an object), are also sometimes known as “instance
variables”
Method Declarations, Headers and Signatures:
What is a Method Declaration?
• The method declaration is the whole bang-shoot, The
entire method.
What is a Method Header?
• The first line of the method declaration.
What is the Method Signature?
• The method name and any parameters.
Related Terminology:
• public: is the access modifier
• static: specifies if the method belongs to the class, or
object of the class
• void/othertypes: this is the types of data that the
method returns
Characters have value:
What is a Non-Static Method?
• A non-static method is a method that we say “belongs to
the object”
• When you instantiate (create) a new object, a chunk of RAM
gets reserved and copies of all the parent methods (and
fields) are written into that space.
• Each object has inherited its own set of methods (this is one
of the foundations of OOP)
• The Gogga object has its own copy of move(), setDirection(),
etc
• ALL of these inherited methods are non-static methods
• You call a non-static method by using the object name
(Gogga bob = new Gogga();
bob.setPosition(5,7);)
What is a Static Method?
• A method that is “global” it is common to all objects.
• Not every object can inherit its own copy of such a method.
• Single instance of this method is held instead by the class
• Static method “belongs” to the class
• ALL the Gogga object that we create sit on the same grid
• setGridSize() method is static because ALL Goggas use the
same Grid
• You call static methods by calling their class name and also
the String class (char upperChar =
Character.to.UperCase(‘a’);)
How Does this Idea Extend to Fields?
• Fields (and even classes) can also be static or non-static
• A class called Dog(), that will create new instances of dog
objects each time it is invoked(run/executed)
, • numberOfLegs field is ALWAYS going to be 4. So, there is
no need for every dogs to have it’s own copy of this field
• It is better for Class Dog() to have single, static field, that
the individual objects would then just reference if needed
• But hairColour would be non-static field, because dogs have
a wide variety of different colours. So, each object would
need its own copy of this field
• Non-static field, or fields that belong to an instance of a
class (an object), are also sometimes known as “instance
variables”
Method Declarations, Headers and Signatures:
What is a Method Declaration?
• The method declaration is the whole bang-shoot, The
entire method.
What is a Method Header?
• The first line of the method declaration.
What is the Method Signature?
• The method name and any parameters.
Related Terminology:
• public: is the access modifier
• static: specifies if the method belongs to the class, or
object of the class
• void/othertypes: this is the types of data that the
method returns
Characters have value: