Rule Definition
When an object is created:
* Memory is allocated to all the fields
* All super class fields are also allocated memory
* All sub class fields, super class fields are initialized .
* The constructor is invoked.
Using a static avoid to create an object that takes resources when it is unnecessary.
Remediation
Make this method static.
Violation Code Sample
class Example {
static double pi = 3.14;
int min (int x, int y) { // VIOLATION
return (x>y?y:x);
}
double getPi() { // VIOLATION
return pi;
}
Fixed Code Sample
class Example {
static double pi = 3.14;
static int min (int x, int y) { // FIXED
return (x>y?y:x);
}
static double getPi() { // FIXED
return pi;
}
Reference
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Related Technologies
JEE
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.