Java fundametals

Encapsulation – put the data into class (capsule) along with the methods to access that data. Keep cohesion – ask yourself – what actions an object of the class will be responsible for. Use responsibility-driven design to determine grouping of the data and actions.

Data hiding – hide the internal implementation details of the class behind an interface (public methods in the class). Make member variables (fields) and internally used methods private and expose only the minimal set of public methods. Benefits:
– you can safely change the implementation without worrying that you will break existing code using that class
– do not access/mutate the fields directly, don’t make them public, because:
a) some fields my be dependent on each other and by direct access the class may get into inconsistent state;
b) methods define possible operations performed on the data – it is easy to test/validate them – make the system work as expected
c) making everything public (especially all fields) would clutter the API
d) all public methods are visible – each needs to be documented
Don’t expose the difference between stored data and derived data, to keep the design decisions hidden (e.g. use getSpeed() instead of calculateSpeed()).

The encapsulated class should expose as little as possible about its internal structures.
There is a class Route with a private array/collection of Position elements. Each position element contains private lat, long, unit type and geometry type.
– It is not a good idea to expose a public getter for a private array of Positions – public Position[] getPositions() – we cannot change that into the List implementation without affecting clients code (do not use names like getDataArray() or getTreeMap() )
– It that case it is better to have public void append(Position position) rather then public setPosition(int i, Position position). append(Position position) makes sure that the set position has the units and geometry uniformly set.
– The getPosition(int i) should return a defensive copy of requested position so that the client does not change the units and geometry via the obtained position reference for that particular position (Don’t expose the implementation details – disallow a client to change the internal states of the objects – which may be used in internal calculations and as a result may give wrong results)
the units/geometry should be set explicitly.

Access control modifiers:
– public (visible to any class),
– default package (visible only within the package)
– protected (package visibility and also visible to any subclasses regardless of the package in which the subclass is defined)
– private (visible only withing that class)

Protected and public are part of API and should be documented.
Class member accessibility:
Defining class – all
Class in the same package – pub, prot and pack
Subclass in different package – pub & prot
Non subclass in different package – pub

Given Circle class with protected ‘r’ property. PlaneCircle extends Circle. PlaneCircle is in different package than Circle.
public boolen isBigger(Circle c){
return (this.r > c.r); //this.r – ok – accesses inherited field; c.r – illegal access; solution – both the same package, or (PlaneCircle c) as parameter.

UML visibility: + public, # protected, – private, ~ package

A class is a set of plan, that can be used to create objects.
A Java object is an instance of a class.
A a = new A(); allocates 2 chunks of memory – one for instantiated object, second (small) for reference variable (ref to the object).

volatile – prevent overriding (multi threading)

Initialization order
In case class Sub extends Base and the Sub class create new instance of Sub class in the main method:
1. all static fields and static initializers in declaration order from Base
2. all static fields and static initializers in declaration order from Sub
3. static main method from Sub (if program started via main method)
Variables are initialized before any non-static methods can be called—even the constructor
If the new Sub() is called from main method then:
4. non-static fields in Base
5. execute constructor body in Base
6. non-static fields in Sub
7. execute constructor body in Sub

The very first thing a subclass constructor does is to call base class constructor (implicitly or via super()/super(args)). After that the rest of the subclass constructor body is executed. It is not possible put any code before super class constructor explicit call in the subclass constructor body.

Declaring an object (doesn’t instantiate the object)
type name (e.g. Date today)

int i; (variable declaration)
abstract void myMethod(); (method declaration)

Instantiating an object (uses new operator, memory is allocated)
name = new + constructor (e.g. today = new Date();)

variable definition:
int i = 10;

method definition:
void myMethod(){
//do sth;

initializing an object – assigning values to the member variables, e.g.:
for the constructor:

Date(int year){

today = new Date(12);

Java “pass by value”, i.e. pass the copy of (reference) value

Java passes parameters to methods using pass by value semantics. That is, a copy of the value of each of the specified arguments to the method call is passed to the method as its parameters.

Note very clearly that a lot of people confuse the “reference” terminology with respect to Java’s references to objects and pass by reference calling semantics. The fact is, in dealing with Java’s references to objects, a copy of the reference‘s value is what is passed to the method — it’s passed by value. This is, for example, why you cannot mutate the (original, caller’s) reference to the object from inside the called method.


ListedList consists of a sequence of nodes, each containing arbitrary data fields and one or two references (“links”) pointing to the next and/or previous nodes. A linked list is a self-referential datatype because it contains a pointer or link to another data of the same type. Linked lists permit insertion and removal of nodes at any point in the list in constant time, but do not allow random access. Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly-linked lists.


One thought on “Java fundametals

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s