6.2 Variables and Methods
As the object-oriented paradigm strictly integrates into Java, the logical roles of variables are broadly classified into three categories depending inherently upon the systematic location exactly where they are declared alongside a class construct.
1. Types of Variables by Declaration Location
The scope and lifespan classification uniquely dictate their generation intervals, duration timeline, and practical usage bounds deeply.
-
Class Variable
- Location: Within the class zone boundaries (Outside methods)
- Technique: Sticking the
statickeyword right in front. - Nature: All created instances collectively share a unified localized memory layout dynamically. Accessible easily without instance objects using
ClassName.variable_name. Ceases upon generalized program closure.
-
Instance Variable
- Location: Within the class zone structurally (Outside methods)
- Technique: General normative variable structure formatting seamlessly.
- Nature: Formulated uniquely dynamically whenever a new standalone instance successfully generates. Suitable specifically whenever an object holds genuinely independent states exclusively. Ceases simultaneously practically when its matching instance perishes effectively.
-
Local Variable
- Location: Contained tightly within an inner
{}brace boundary including method sequences, constructors, or structured blocks structurally. - Nature: Comes physically alive functionally when execution runs over its initialization mark. Gets instantly permanently deleted upon systematically exiting explicit boundaries effectively entirely preventing later use gracefully.
- Location: Contained tightly within an inner
Comparing Example Scopes Code
class Card {
String kind; // Instance variable (Suits vary fundamentally per card item)
int number; // Instance variable (Values significantly vary precisely per card)
static int width = 100; // Class variable (Card physical width dimensions universally mutually shared uniformly)
static int height = 250;// Class variable (Heights strictly shared evenly)
void method() {
int lv = 0; // Local variable (Functionally available localized effectively inside bounds only)
}
}
2. Methods
A Method is conceptually binding sequences of statements logically meant functionally performing concrete specific operative tasks systematically effectively. It retrieves specific input parameter materials logically, computes inside securely instructed exactly, then safely functionally returns calculated answers directly passing to its caller neatly.
Crucial Utility and Why
- Exceedingly High Reusability: Readily utilizing perfectly developed programmatic components dynamically recurrently calling anywhere appropriately essentially effortlessly.
- Trimming Code Duplication: Extracting repetitive implementation processes into localized modules practically ensures code neatness fundamentally tightly.
- Program Structuring Module Frameworking: Mitigating messy monolithic functions by distinctly chunking logical sequences effectively guaranteeing modular architectures.
Returning Types & Execution Parameters
Java robustly forces stipulating returned format types firmly preceding exact terminology function labels accurately. Correspondingly logically, raw ingredients practically necessary for functional processing sit within formatted parameters neatly inside (). Utilizing the void identifier effectively denotes functionally missing return value requirements inherently.
// Logic formally adding integers, ultimately enforcing an 'int' standard explicitly returned natively
int add(int a, int b) {
return a + b;
}
// Strictly acting structurally natively explicitly forbidding (voiding) returned content inherently
void printWelcome(String name) {
System.out.println("Hello fundamentally, " + name);
}
Realistically methods act merely systematically outlining logic natively. You unconditionally require calling its function functionally deploying proper explicit invocation parentheses () structurally accurately effectively running mapped mechanisms successfully.