Section 9d

Scope Considerations with Classes


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Identifying Available Components

Class vs Local Scope

In the simplest of terms, scope is defined as when any Java code component is available for use. Variables, constants, objects, or anything else declared within a method can only "live" or be accessed in that method. Items declared within a method are commonly said to be in local scope. Probably the simplest way to think about what is in scope is to say that it is available to anything within the curly braces in which it was declared. There will be a little more to it soon but this is a good place to start. Consider the following code snippets from the program provided in the previous section.

public static void main( String[] args )
{
// initialize program

// initialize variables
Console_IO_Class mainIO = new Console_IO_Class();
double sideOne, sideTwo, hypotenuse;

// . . . other method code
}

public static double getTriangleSide( String prompt )
{
// initialize method, variables
Console_IO_Class triangleSideIO = new Console_IO_Class();

// . . . other method code
}

public static void showResults( double side_1,
double side_2, double hypotenuse )
{
// initialize method/variables
Console_IO_Class resultsIO = new Console_IO_Class();

// . . . other method code
}

public static void showTitle()
{
// initialize method/variables
Console_IO_Class titleIO = new Console_IO_Class();

// . . . other method code
}

Notice that a new object of the Console_IO_Class is created in each method. They all have different identifiers but they could all be conIO or something equivalent. The name does not matter but the scope does. Each of these object references is local only to the method, or curly brace pair, within which they are declared. titleIO cannot be used in showResults and mainIO cannot be used in getTriangleSide. These are all examples of using local scope. However, this could have been done differently, as shown here:

public class HypotenuseClassNew
{
private static final int TWO_END_LINES = 2;
private static final int PRECISION = 2;
private static Console_IO_Class classIO = new Console_IO_Class();
public static void main( String[] args )
{
// . . . method code
}

public static double calcHypotenuse( double sideA, double sideB )
{
// . . . method code
}

public static double getTriangleSide( String prompt )
{
// . . . method code
}

public static void showResults( double side_1,
double side_2, double hypotenuse )
{
// . . . method code
}

public static void showTitle()
{
// . . . method code
}
}

Declaring the Console_IO_Class object classIO within the class scope (i.e., between the class curly braces) means that it can be used everywhere in the class, including in each of the methods that need it. The identifiers for each method would all need to be converted to classIO but then there would be no need to declare separate objects in each of the I/O related methods. Note again that the class had to be declared as static (for reasons discussed previously) and as private (for reasons discussed next).

This is your first interaction with the concept of scope but it won't be your last. You must consider scope throughout your programming activities

Public vs Private Scope

Two of the more critical characteristics of OOP are encapsulation and data hiding. Just like the table saw you purchased to build your house, there are parts within it such as the table, the motor and saw blade, the table legs and feet, and so on. These are packaged or encapsulated into a thing you call a table saw which you can use, thus making the table saw available to you and they are said to be public.

To extend on the metaphor, there are parts inside the motor that you should not manipulate. As an example, there are brushes and bearings inside the motor that could be harmed if untrained individuals opened them up. These parts are therefore not accessible by the table saw user and would be considered private. This is another example of how object oriented programming is comparable to real world circumstances.

To continue extending the metaphor, there are times that the brushes need to be replaced. So in some cases, a person authorized to work inside the table saw has access to the motor brushes and she can replace them. She is said to be an accessor or mutator in the table saw class system. So while you cannot touch the brushes yourself, you can call a public method named replaceBrushes and the appropriate component within the class can manage the private brushes for you.

Extending this to data should be pretty easy. If you wish to copy files on your computer, you should be able to call or click on a public process that will copy this given file to that given directory. On the other hand, there are some operations involved in copying that involve opening the file, getting a file handle, managing the bytes in bulk and making sure all the data lands in the right place on the hard drive, and then closing the file and cleaning things up; these are parts of the process very few people are capable of doing. Those operations must stay private.

Finally, in the car list class mentioned earlier in this chapter, there is a chance that users could mix one car up with another car's color, incorrectly identify an engine, or miscount how many cars are in the list. This data is commonly hidden or private but users can still ask for a list, or a specific car, or set of cars of a certain color, by using the public methods that provide this service.

In Java, every component of a class whether it is data or methods will be configured as public or private as appropriate for the class; these terms are called access modifiers. As a note, if a programmer does not set this, the compiler commonly sets the state of the data or method to some default condition. A common one is package private which means the data or method is accessible to all the classes within the package but private to any classes not located in that package. While Java can handle this for you, it is better to set all your class-level data and methods to public or private as appropriate so they are assured to be configured appropriately.

While there will be many more examples as this reference continues, a quick example can be provided in the Console_IO_Class. There is a method called promptForDouble that is meant to be available to the user/programmer (you). However, within that method are a couple of others such as getInputString (that captures all the input characters from the keyboard), and isDigit (that verifies that a given character is between '0 and '9', inclusive) that provide support to the method but are not appropriate or necessary for use outside the class. getInputString and isDigit are therefore private while promptForDouble is public.

As mentioned, while this is a pretty comprehensive treatment of scope, there will be more as you continue your programming journey. Stay Tuned.


Watch this video to learn more about scope management within classes.