Java: Berbagai Istilah
Tulisan ini menjelaskan berbagai istilah yang sering digunakan di Java
Definisi
Sangat penting bagi kita untuk mengerti struktur dasar dari package, class dan object.
Dasar: Package, Class dan Object
Package
Java mengelompokan class menjadi package-package fungsional. Semua class di java.lang akan secara automatis tersedia di java program.
Tujuan utama akan adanya package adalah untuk menghindari adanya tabrakan nama. Seorang programmer mungkin akan membuat java class Test. Programmer lain mungkin akan membuat class dengan nama yang sama. Dengan menggunakan package kita dapat memberitahukan system class yang mana yang akan di panggil. Contoh, jika programme1 meletakan class Test di package programmer1 dan programme2 meletakan class-nya di package "xmlreader" maka kita dapat mengakses class ke dua tersebut melalui xmlreader.Test.
Salah satu alasan penting lainnya adalah melalui package kita dapat mengelompokan class kita menjadi unit logik.
Class
Defini: Sebuah template yang menjelaskan data dan perilaku yang berkaitkan dengan instance dari class tersebut.
Data dari dengan sebuah class di simpan dalam variable; perilaku dari sebuah class atau object di implementasikan dalam method.
Semua dapat di lihat dalam blueprint dari object.
Sebuah class
- Di definisikan dengan kata kunci class.
- Di mulai dengan huruf besar.
- Badan class di tutup dengan {}
Object
1.1.3. Object
Def.: An object is an instance of a class.
While a class can be considered as a blueprint for the data and the behavior of an object the object is the real element which has data and can perform actions.
For example as class may be the blueprint of a car, an object is then a real car.
Every object extends implicitly the Object “Object” and get wherefore gets the methods defined by class Object
o.equals(o1) Is the Object equal to o1
o.getClass() Returns the class of o
o.hashCode() Returns a unique ID o.toString()
1.2. Variables 1.2.1. Variable
Variable can either be a primitive or a reference variable. A primitive variable contains value while the reference variable contains a reference (pointer) to the object. Hence if you compare two reference variables then you compare if both point to the same object. To compare objects use object1.equals(object2). 1.2.2. Instance variable
Instance variable is associated with an instance of the class (also called object). Access works over these objects.
Instance variables can have any access control and can be marked final or transisient. Instance variables marked as final can not be changed after assigned to a value. 1.2.3. Local Variable
Local (stack) variable declarations cannot have access modifiers.
final is the only modifier available to local variables
Local variables don't get default values, so they must be initialized before use. 1.3. Modifiers 1.3.1. Access Modifiers
There are three access modifiers: public, protected and private
There are four access levels: public, protected, default and private
protected and default are similar. Only difference: A protected class can get accessed from the package and sub-classes outside the package while a default class can get only access via the same package protected = package plus kids 1.3.2. Other modifiers
final methods: cannot be overwritten in a subclass
abstractmethod: no method body
synchronized method: threat safe, can be final and have any access control
native methods: platform dependent code, apply only to methods
stricttp: class or method 1.4. Interface
Within java an interface is a type just as a class is a type. Like a class an interface defines methods.
Interfaces are contracts for what a class can do but they say nothing about the way in which the class must do it.
Interfaces are per default public and abstract – explicit declaration of these modifiers is optional.
An interface can also have abstract methods, no concrete methods are allowed.
Interfaces can have constants which are always implicitly public, static and final. 1.5. Additional Definitions 1.5.1. Method
A method can trigger a certain action.
Method with var-args: Method declares a parameter which accepts from zero to many arguments (syntax: type .. name;) A method can only have one var-args parameter and this must be last parameter in the method.
Overwrite of a superclass method: A method must be of the exact same return parameter and the same arguments. Also the return parameter must be the same. Overload methods: An overloaded method is a method with the same name, but different arguments. The return type can not be used to overload a method. 1.5.2. Constructor
classname (Parameter p1, ..) {}
The constructor is always called if the class is created. If no explicit constructor is defined the compiler adds implicitly a constructor. If the class is sub-classed then the constructor of the super class is always implicitly called. 1.5.3. Class method or class variable
Class method / variable is associated with the class and not the instance. To refer to it use the classname and the class method / variable together with a period ("."). Example System.out.println("Hello World"). The runtime environment associates one class variable for a class no matter how many instances exists.
Class method / variant does exists once for all instances. They are indicated with the word static. If a variable should be existing for all instances and be non-changeable (=CONSTANT) then also the word final is used.
The static method runs without any instance of the class. Does not depend on instance (non-static) variables. Can not use the this object or an instance variables.
The static variable is the same of all instances of the class (global variable) 1.5.4. Abstract class
If a class has one method which only contain the declaration of the method but not the implementation then this class is abstract and can not be initialized. Sub-classes need then to define the methods except if they also declared as abstract. method abstract double returnDouble();
If a class contains an abstract method it also needs to get defined as abstract.