Yes, perhaps you can use the block anywhere you can use a separate statement. Variables declared in this block will be valid only within the block. For example:.
void method() { String allThisCodeCanSeeMe;
But . Usually, if you want to do something like this, this assumes that you need to break the code into smaller functions / methods and have what your method is currently being called the less part:
void method() { String thisMethodCanSeeMe; // ... this.aSmallerMethod(); // <== Can pass in `thisMethodCanSeeMe` this.anotherSmallerMethod(); // <== if needed // ... } private void aSmallerMethod() { String onlyThisMethodCanSeeMe; // ... } private void anotherSmallerMethod() { String onlyThisSecondSmallerMethodCanSeeMe; // ... }
Tj crowder
source share