0% found this document useful (0 votes)
2 views9 pages

Module 5 - Static Methods

The document provides an overview of Java static methods and fields, highlighting their shared nature among class instances and their limitations. It explains the difference between instance and static methods, emphasizing that static methods cannot access instance variables or non-static methods. Examples illustrate the utility of static methods for tasks like unit conversion, where instance-specific data is unnecessary.

Uploaded by

net18nt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Module 5 - Static Methods

The document provides an overview of Java static methods and fields, highlighting their shared nature among class instances and their limitations. It explains the difference between instance and static methods, emphasizing that static methods cannot access instance variables or non-static methods. Examples illustrate the utility of static methods for tasks like unit conversion, where instance-specific data is unnecessary.

Uploaded by

net18nt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Static Methods

COP 2250/2210

This presentation contains adapted materials from different sources, including our
designated textbook, to be used for educational purposes only
Summary

• Review of Instance Fields and Methods


• Static modifier: static fields
• Static Methods
Review of Instance Fields and Methods
Dog Class
• Each instance of a class has its own copy Attributes: Methods:
of instance variables. weight Bark
• Example: Name Jump
Size
• The Dog class defines a color, name, and
size field.
• Each instance of the Dog class can have
different values stored in those fields

• Instance methods require that an instance Pug


of a class be created in order to be used Labrador Setter

• Instance methods typically interact with


instance fields or calculate values based
on those fields
Pug Object Labrador Object Setter Object
Pug weight Pug weight Pug weight
• New and distinct space is created in Pug name Pug name Pug name
Pug size Pug size
memory for instance fields and methods Pug size
----
every time we instantiate an object ---- ----
Pug bark Pug bark Pug bark
Pug jump Pug jump Pug jump
Static modifier: static fields
public class Countable
{ instanceCount is
private static int instanceCount = 0; now shared among
all instances
public Countable()
The constructor increments
{ the static field
instanceCount++; instanceCount. This keeps
track of the number of
} instances of this class
that are created.

public int getInstanceCount()


{
return instanceCount;
}
}

• An item that is static is shared among all objects of the class


• Memory is allocated only once for static fields and methods
• The field is initialized to 0 only once, regardless of the number of times
the class is instantiated.
• Primitive static fields are initialized to 0 if no initialization is performed.
Static Methods
• A static method is shared among all instances of objects and
you do not have to create an object to use it. They are
useful for utilitarian purposes.
public static double milesToKilometers(double miles)
{…}
• we see this with the Math class – we did not need a Math object,
instead we could do [Link] or [Link]

double val = [Link](25.0);

Class name Static method


Note: In UML static methods are underlined
Static Methods Limitations
• A static method can access only static data. It can not access non-
static data (instance variables)

• A static method can call only other static methods and can not call a
non-static method from it.

• A static method can be accessed directly by the class name and


doesn’t need any object

• A static method cannot refer to "this" or "super" keywords in


anyway

• A static methods can not be overridden.

• An outer class (top level) can have static methods but cannot be
declared as static
Static Methods
• Static methods are very similar to functions in
procedural programming (non OOP) since we
loose most object oriented functionality within
them.
• Use them only if:
– You want to reuse the same exact code among all
objects of a class and the static method will never
need to access instances.
– If you are sure that the definition of the method will
never be changed or be overridden.
Static Method Example
public class Metric
{
public static double milesToKilometers(double miles)
{
return miles * 1.609;
}

public static double kilometersToMiles(double kilometers)


{
return kilometers / 1.609;
}
}
• In this example where we need to simply convert a value from miles to kilometers and
vice versa
• It would make sense to create static methods for these tasks in order to avoid duplicate
code.
• It is assumed that the code here would never change (objects would not need to make
changes or updates to the method).
• These “utilitarian” and “static” functions, are independent from instance objects
Static Method Example 2
public class DriverClass {

int instX=1, instY =2; //instance variables

public static double example1 (int x, int y) {


double z=(double)x/y;
return z;

public static void main(String[] args) {


int localX=1, localY=2; //local variables

//WORKS with local variables


[Link](DriverClass.example1(localX, localY));

//DOES NOT WORK with instance variables


[Link](DriverClass.example1(instX, instY));
}
}

You might also like