Java Variables, Objects, Static and Instance Members
Java Variables and Object Concepts
0
Four kinds of variables
There are four kinds of variables:
- Local variables are those declared in method bodies; these appear in the variable portion of a Java execution frame. Unless explicitly requested, you generally do not need to show execution frames in an object diagram.
- Instance variables are non-static variables declared within a class body; these appear within an instance object box (see the discussion of objects below).
- Class variables are static variables declared within a class body; these appear within a class box. Only one copy of the variable exists regardless of the number of objects.
- Indexed variables are numbered variables that appear within array objects; see the discussion of array objects below.
Instance objects
An instance object is an object constructed from the template of a particular class. The variables within an instance object are the instance variables of the class. The label of an instance object is the name of the class from which the instance was derived. Suppose the Square class has the following instance variables:
- An integer
sidespecifying the side length of the square. - A
Pointobjectcornerspecifying the upper left-hand corner of the square in a two-dimensional grid. - A boolean
isFilledspecifying whether the square should be drawn filled or not.
Important details
Important: The only way to know what instance variables should be in an instance object is to examine the definition of the class of that instance. However, be careful: some uses of new are hidden. For instance, creating the String object 'Georgia' involves a hidden use of new. The null reference is the default value for a variable that contains an object. In an object diagram, a variable containing the null reference is drawn as a box with a slash.
Array objects
Array Objects
Command-line arguments
Main takes input args; these arguments are called command-line arguments.
Constructor overloading
Constructor overloading means multiple constructors can exist in a class with different parameter lists.
Encapsulation
Encapsulation hides class variables and internal implementation details from users; only the class’s own methods should access its private data.
Static
Static fields (variables) – definition
Static variables are class-level variables shared by all instances of the class. For example, if a class has two instances obj1 and obj2, they both access the same static variable.
Memory allocation and initialization
Memory is allocated to static variables when the class is loaded by the JVM. Static variables are initialized during this memory allocation, i.e., when the class is first loaded.
Access and usage
Since static members are the same for all instances, they can be accessed using the class name without the need for any instances.
Static methods – definition and limitations
Static methods belong to the class rather than any specific instance. They can be accessed using the class name. Important limitation: static methods cannot access non-static variables or non-static methods directly, because static methods are not tied to any instance.
Non-Static
Instance fields (variables) – definition
Instance variables are tied to instances of the class. Memory for instance fields is allocated when a new object of the class is created; they are initialized when the object is instantiated. Instance fields are accessed using instances of the class.
Instance methods – definition and capabilities
Instance methods are tied to instances of the class and are accessed using instances. Unlike static methods, instance methods can access both instance variables and static variables.
Differences between static and non-static
- Tied: Static members belong to the class; non-static members belong to instances (objects) of the class.
- Access: Static members can be accessed using the class name without creating any object; non-static members must be accessed using an object of the class.
- Memory allocation: Static members are allocated when the class is loaded; non-static members are allocated each time an object is created.
- Initialization: Static members are initialized when the class is loaded by the JVM; non-static members are initialized each time a new object is created.
- Scope: Static members have class-level scope; non-static members have instance-level scope.
- Access to instance members: Static methods cannot access instance members directly; instance methods can access both instance and static members.
Wrapper classes
Wrapper Class – A wrapper class wraps a primitive type value in an object. For example, the primitive int has the wrapper Integer, and double has the wrapper Double.
Notes about exceptions, input formatting, and special values
A program that does not handle an exception ends execution.
Wrapper objects can be null because they are objects.
Only inputs that contain more than three digits should contain commas to separate every third digit.
0.0 / 0.0 evaluates to NaN, so the variable will equal NaN.
Exception is the base class of all exception types, so a catch block with an Exception parameter type can catch all exception types.
A throws clause specifies the types of exceptions that a method may throw and that callers of the method should handle. A throws clause is specified after a method’s parameter list and before the opening brace. A throws clause may specify multiple exception types, separated by commas.
Two types of exceptions
- Checked exception: A checked exception is an exception that a programmer is expected to handle within the program. If a method includes code that may throw a checked exception, the Java compiler ensures that the method either catches the exception or declares that the exception is thrown. Example: a program that opens files should anticipate and handle
FileNotFoundException. - Unchecked exception: An unchecked exception is an exception that a programmer is not expected to catch and handle at runtime. The Java compiler does not ensure unchecked exceptions are handled by a program. Example: a program should try to eliminate code that uses null references instead of catching and handling
NullPointerException.
