Java Class Information

See also SUNs tutorial on Classes and Inheritance.

Java Instance Variable and Methods

A class in Java contains class variables, instance variables, class methods and instance methods. We will work with instances first.

The MyClass contains an instance variable i and two methods, MyClass which is a constructor for the class, and Add_to_i(int j).

Methods can be called by other classes(if public) or within the same class. One specific type of method is a constructor and

When you declare a class in Java, you may declare one or more optional constructors that perform initialization when you instantiate an object of the class name.

Consider:

In this way, Java allows "overloading" of methods. Note this is not the same as overloading of functions in C, C++. The first step is looking at the object instance, then the method is always determined and performed based (1) on this object instance (2) on how many arguments were passed (3) on the type of arguments.

Consider: Before a class is instantiated with the new keyword it does not use any memory resources, it is merely a type declaration. What happens when it is instantiated? Here is more information on what Java does when an instance is instantiated.

After the new object mc is instantiated, the value of i will be equal to 10. You may refer to the instance variable i with the object name:

Since mc has all of the variables and methods of MyClass, we use the same syntax to call the method Add_to_i by using our new instance name mc: now the mc.i instance variable is 21
will instantiate with the correct one.

Another aspect: Now, in some other method (in another class) we see:
n.first = ? First, does the object in question know n? If the instance does not have a handle to n, then it cannot get n's variable values. So, either

  1. n had to be instantiated in the method asking information about it OR
  2. n could be a variable of the class in which this method is defined (i.e., the method asking to find out about n.first).
  3. n was passed to the method

Even with this access to the handle, in general, classes won't know about other classes variables since instance variables should be private to classes (encapsulation).

One can make variables public but one should do so only for variables (and methods) that form part of the public API of the class.

If you are inside a method being defined in Name then it knows the variables of Name (even the private ones).

As a practice, instance variables should be always private to ensure data hiding. Then one supplies accessor and mutator methods. It is a good idea to use the pattern getIVname() and setIVname() since it makes your design obvious and it is used by the Java Reflection Package to do cool things. (see Beans)

The keyword public means that any method in any class that has access to an instance of the class can call the method.

private String name;
The private keyword makes sure that no outside agency can access the instances fields except through the methods of our class.

Note, in this class the instance variable is a String (which is a Class).

gives me the value of the IV's state of the object instance which variable represents which is an IV of the object instance myObject.
What?
We go from the left to the right. First myObject.variable determines the value of the variable of myObject (call it gotit) then we get the value of the variable state from gotit

Now, this is interesting. One should be able to assign values to these variables:

Does myObject have permission to change IV's of the object in variable?
(1)consider handles (2) permissions: Think about it before you access
this test

Methods

A handy pair of methods is:myObject.getclass().getName()
Another handy tool is the instanceof predicate

Programming style:

Since it is significant, I will repeat:

If you really want to have data encapsulation and data hidding, then you probably want your variables to be private.

But, if variables are private, then how can you do techniques to access information?

One clean methodology is to always program accessors and converting (or mutating) methods (in LISP we had GetValue and PutValue). Most pure object-oriented languages make you provide such accessor methods.

Thus a user can only access the Instance Variables through the methods - and cannot do things like mc.i++

Finalizers

Java does not support destructors (as in C++) since it provides a means of automatically reaping objects that go out of scope, but it does provide a method that the automatic garbage collector will call:

This tells the garbage collector to do it but it does not actually do it until the garbage collector gets it.

You can always call the finalize method yourself, but calling it does not force the object to be garbage collected...only removing all references to an object will cause it to be marked for deletion.

Finalizer methods are best used for optimizing the removal of an object. As in the above example, by cleaning up things that the object may have used or by removing references to other objects.

When the last thread dies, the program stops. What does exit do? Kills whatever threads are present. Garbage collector is a thread.

See also SUNs tech tip

Another simple example of lost objects:

We created two strings objects, "abc" and "def". By the end of the third statement, the first object we created named s (that contained "abc") has gone out of scope. There is no way to access it anymore. It is automatically marked and cleared in the next iteration of the garbage collection thread.

Forced finalization and gc
(System.runFinalization() (calls finalize on all objects waiting to be g.c.), System.gc())

Subclassing (inheritence)

Java allows only single inheritence.

Subclassing allows us to create a new object defined in terms of some existing object.

In the HelloWorld class for applets we saw:

The keyword extends denotes subclassing. HelloWorld is a subclass of Applet.

Since the class Applet is contained in the package java.applet we need to import java.applet.Applet; so that we have access to the class

Public - we will get to permissions in classes in a bit ...

A new class with nothing in it is pretty useless. In any class we set:

The Methods can be totally new, can wrap-around parent's methods, or can override parent's methods

Class Variables and Methods

Class variables are initialized when the class is first loaded. Instance variables are initialized when an object is created.

Class variables have the same value for all instances of a class. In Java they are denoted with the keyword static.

Note: to get or change the value of a class variable, you can use either the instance or name of the class on the left of the dot.

super and this

this refers to the "current" instance (like self in Smalltalk)
super is the parent of the "current" class

Consider myClass again:

Here this.i refers to the int i in the class MyClass

default: if don't say who gets message, it is this.whatevermessage

If you need to call the parent method within a class that has overridden that method, you can refer to the parent method with the super keyword:

In the following code fragment, the value of i would be set to 10 by the constructor, then set to 15 and finally to 25 by the parent (MyClass) Add_to_i method:

Consider the various uses of the variable i. Note, this is not good programming style, but you should know scope. Now do:

Permissions, ClassTypes, Inheritence, Interfaces, Packages

Permissions (or Protections) depends on point of view :-) :

for methods and variables

Typical order:

<access> [static]* [abstract]* ...

P's : public, private, protected, "package"

Modifier Visibility (page 148, Exploring Java; page 73 Nutshell)
public All classes
private None (only within own class)
protected Classes in package and subclasses inside or outside package
none (default) Classes in same package

Another example about who can access what (class vs instance variables and methods):

Elsewhere if a variable is an instance variable, there are copies in each object
if a variable is a class, there is only one copy

Note:

If you declare a method static, it prevents the method from accessing any variables of the class which are not also declared static (i.e., class methods cannot change instance variables):

Class Types:

abstract
An abstract class must have at least one abstract method. An abstract class cannot be instantiated, and must be subclassed
final
A final class is declared as the last class of any subclass chain. (efficiency) Classes declared final may not be subclassed. The Math class is a final class
public
Can be accessed by other classes, either directly or subclassing. The class must be imported to be used by other packages, otherwise it is only available within the package where it is declared
synchronized
This class modifier declares that the methods defined in the class are all synchronized.
Modifiers for methods and variables
final
A final method cannot be overriden, a final variable may never have its variable set
native
native is a modifier for methods. It indicates that the method is implemented elsewhere in C, or in some other platform-dependent fashion
transient
indicates a field that is notpart of an object's persistant state and thus will not be serialized with the object
synchronized
This class modifier declares that the methods defined in the class are all synchronized.
volatile
applied to variable fields. Specifies that the filed is used by synchronized threads and that the compiler should not attempt to perform optimizations with it.