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
Java Instance Variable and Methods
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.
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:
Another aspect: Now, in some other method (in another class) we see:
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)
private String name;
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? Methods
A handy pair of methods is:myObject.getclass().getName()
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++
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.
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:
Forced finalization and gc
Java allows only single inheritence.
Subclassing allows us to create a new object defined in terms of some existing object.
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 ...
The Methods can be totally new, can wrap-around parent's methods, or can override parent's methods
Class Variables and Methods
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)
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:
Permissions (or Protections) depends on point of view :-) :
for methods and variables
Typical order:
P's : public, private, protected, "package"
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
The keyword public means that any method in any class that has
access to an instance of the class can call the method.
The private keyword makes sure that no outside agency can
access the instances fields except through the methods of our 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
(1)consider handles (2) permissions:
Think about it before you access this test
Another handy tool is the instanceof predicate
Programming style:
Since it is significant, I will repeat:
(System.runFinalization() (calls finalize on all objects waiting to be g.c.), System.gc())
Subclassing (inheritence)
super is the parent of the "current" class
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
(for classes, one usually starts out with this until know why to protect)
Also known as "package" since it allows access to any objects inside the same 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):
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):